![]() |
To handle loading states and asynchronous dependencies we can use Suspense Component provided by React. When we load a component lazily, it loads only when it’s needed and it takes some time to load it. During this period, when a component is loading there must be a fallback UI in place of it like a loading spinner or any other placeholder content like a Shimmer UI. Steps to Setup React App:Step 1: Create a React application and Navigate to the project directory with the following command: npx create-react-app my-project Project Structure:![]() Project Structure The updated dependencies in package.json file will look like. "dependencies": { Handling loading states in lazy loaded components?First, we need to load a component lazily after that we can use Suspense Component where was can pass fallback UI as a prop and inside Suspense Component we can render our lazy loaded component. Example: Below is the code example
Output: ![]() Loading States Handling asynchronous dependencies in lazy loaded component?First, we need to load a component lazily, since we need an asynchronous dependency we will fetch some random data and when that data gets loaded we will load the lazy loaded component. That means our lazy loaded component is dependent on some asynchronous task. Example: Below is the code example:
Output: ![]() Asynchronous Dependencies |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |