State Management
Managing context, state and hooks
Last updated
Managing context, state and hooks
Last updated
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
We are using context for login methods - Auth0, JWT, Firebase, AWS.
SWR is a popular library for data fetching and state management in React applications. Data fetching is covered in section, here we are going to cover state management using SWR.
Mantis is managing the state of following using SWR
Snackbar states
Menu states
E-commerce Cart
Snackbar is used to show notifications in the application. Whenever a component dispatches an action with data to show snackbar, it appears in the app. The initial state of snackbar is being captured using useSWR hooks and after that, we are mutating it state based on action calls. Following is the initial state:
This values have been initialised using useSWR like below:
Now how to read these values and update them as well.
To read the values and listen, following SWR hooks is sufficient in component. Whenever any changes happen, it activated like a redux selector and act based on logic.
To open the Snack bar, we need to change the value and mutate the state. Here is how it is done
Then the method openSnackbar will be called from components to open snackbar.