The web content provides a comprehensive guide on implementing Redux-persist with Redux-toolkit for state persistence in React Native and React JS applications.
Abstract
The article titled "Redux-persist & Redux-toolkit implementation made easy for React Native and React JS" offers a step-by-step tutorial on integrating Redux-persist with Redux-toolkit in a React Native counter application, which is also applicable to React JS web apps. It explains the benefits of using Redux-persist to maintain the application state across sessions and the advantages of Redux-toolkit for simplifying Redux implementation. The guide covers setting up a React Native application, implementing Redux with Redux-toolkit, and then adding Redux-persist to achieve persistent storage of the Redux store. It also includes a bonus section on using React Native Paper for Material Design components and provides references to a live example, GitHub repository, and other resources for further understanding.
Opinions
The author assumes the reader has a good understanding of React JS or React Native and a basic knowledge of Redux.
Redux-toolkit is highly recommended by the author for its simplified approach to Redux implementation, including built-in support for Thunk and redux dev-tools.
The article emphasizes that the documentation for Redux-persist is more tailored to traditional Redux implementations and aims to fill the gap for those using Redux-toolkit.
The author provides a subjective opinion that the implementation shown is sufficient for industry-level corporate standard applications.
The author expresses a personal view that Material UI for React JS and React Native Paper for React Native are very handy for following Material Design guidelines.
A minor critique is noted regarding the missing dropdown/select element in the React Native Paper library as of the publication date.
The author encourages readers to show appreciation for the article by buying them a coffee, indicating a preference for direct support from the community.
Redux-persist & Redux-toolkit implementation made easy for React Native and React JS
(redux-persist with redux-toolkit in react)
updated: Oct 02, 2021
Redux-persist does a very simple yet very powerful job of persisting your application state across different user sessions /browser windows(days, months, years).
Why this Story?
✓ Implement redux-persist in a react (JS or native) app with redux-toolkit.
✓ You may find documentation and other internet articles for redux-persist implementation with traditional redux (store, Provider, reducer, actions)
✓ If you are using the now recommended redux-toolkit (configureStore, createSlice, Provider), you may not find much help to implement redux-persist.
Redux-persist helps us to save the redux store state across the application sessions. You kill the app/browser and open again, your redux store is persisted.
In this article, we will mainly focus on implementing redux-persist with “redux-toolkit” (redux-toolkit includes Thunk and redux dev-tools by default, removable though.)
Redux-toolkit is now highly recommended to simply redux implementation in your react web and native applications.
Live example on expo snack (play around and refresh the window, it will always retain your counter value on the same device/browser)
This example will also help you with the basic implementation of the redux-toolkit. This implementation should be sufficient for an industry-level corporate standard web / react-native application.
For React Js Corporate level app architecture and design pattern in detail refer:
https://saurabhshah23.medium.com/70b7b9103f22
Example scenario:
Suppose you have a simple counter app.
This app displays the current value of the counter (default=0), and two buttons for Increment and Decrement.
A user increments and the current value is 10.
For some reason, your user kills the app. Next time that user opens the app, the app will display the default counter value as 0 and not 10.
You wish to locally persist all the app’s redux store values on that device across the user sessions without sending/saving them to the database and fetching on app startup.
You need redux-persist.
Implementation:
Step-1: React native counter application.
✓ This is a basic counter-example.
✓ A numeric value is displayed.
✓ Two buttons for Increment and Decrement the counter value are displayed.
✓ App is created using the “npx react-native init ReduxPersist” command.
✓ Checkout “README.md” for all the dependency installations.
✓ You can install all the dependencies at once and then follow further steps. Remember feel free to play around.
Step-2: Redux implementation using redux-toolkit.
I will try to cover this in a little depth for covering “redux-toolkit” usage and understanding.
Do not hesitate to skip this part if you are already aware of redux-toolkit.
1. Get familiar with the directory structure:
a. All the code resides within the “src/” folder for redux and components.
b. The main configuration code resides in “/App.js”. You can move this to a separate file as well. I have kept it like this to avoid many changes from the default react-native init boilerplate code.
2. Create a Redux Store:
a. Create a store using the “configureStore” method from redux-toolkit.
b. It is way too simplified from the traditional implementation not requiring combineReducers, createStore, middleware, configuring redux developer tools, etc.
c.Redux-toolkit comes packed with Thunk and redux dev-tools.
d. In the above image, we have already added counterReducer, which will be created in the upcoming step via createSlice method.
3. Provide the Redux Store to React:
a. Wrap the application JSX within the redux Provider.
b. This code is different from what you see in the live example, as we will be amending the code to implement redux-persist.
4. Create a Redux State Slice (reducer):
a. Redux-toolkit takes away the hassle of defining actionTypes, creating actions, defining reducer cases for each actionType, etc in a single and simple “createSlice” method. Don’t believe it, that’s why redux-toolkit is recommended.
b. Async-storage is for react-native mobile apps. Although it works on browsers, you can skip and use storage and it will default to localStorage for React JS web apps.
c. “combineReducers” is the traditional method required if you have more than one reducer.
d. OK, I remember I lauded redux-toolkit to not require combineReducers, but unfortunately this is the only way-around for multiple reducers.
You won’t find these things in the documentation.
e. Blacklist any store attribute using its reducer name in the “persistConfig” object. Blacklisted attributes will not persist.
f. Blacklist option itself is sufficient for most scenarios, although there is a whitelist option as well for exactly the opposite function (persist only whitelisted attributes).
g. Add …ignoredActions:[…] in configureStore() method. This is specifically to handle a non-serializable value error.
b. Wrap the app jsx within PersistGate to launch app with the previous known application state (redux store)
c. The PersistGate “loading” prop can be null. In our example, we are displaying a loading indicator while the store is being reinstated.
That’s all folks!
Now, play around. Increment and decrement the values for counter value to be any number.
Kill the app explicitly, and restart.
Your app should launch with the counter value to be the last updated value and not zero.
BONUS:
React Native Paper
React Native Paper is a high-quality, standard-compliant Material Design library that has you covered in all major use-cases.
I personally feel Material UI for React JS and RN Paper for React Native come in very handy to follow the material design.
Con: An only missing component as of today (published date) in this library I feel is the dropdown / select element.
But, then I found it is the case with all the other React Native libraries like elements.