How to use Redux with AsyncStorage
https://hackernoon.com/hn-images/1*VeM-5lsAtrrJ4jXH96h5kg.png

How to use Redux with AsyncStorage

First of all, may you’re wondering why should you or anyone use the Redux with AsyncStorage, because is easier to use the Redux with AsyncStorage and you can just worry about the Redux and his state will persist normally, which means the user can close his phone and the state still will be saved, I’ll show you.

However, what is Redux? The Redux is a predictable state container for JavaScript apps. Commonly the Redux and React are using together because React commonly are using, manipulating, passing to another component a lot of states and it could be a little hard to manipulate and maintain.


We’ll use the React native with the Expo.

1. We’ll start the expo template with Typescript

npx expo init -t expo-template-blank-typescript reduxWithReact

Your repository must look like:

N?o foi fornecido texto alternativo para esta imagem

We’ll create a folder called src and inside that an index.tsx file and a folder called store now it’ll look like:

N?o foi fornecido texto alternativo para esta imagem

The App.tsx should look like:

N?o foi fornecido texto alternativo para esta imagem


Now we’ll install some dependencies to use the Redux and the persistor to through it use the AsyncStorage with the Redux.

yarn add react-redux @types/react-redux redux redux-persist reduxsauce seamless-immutable @types/seamless-immutable @react-native-async-storage/async-storage redux-saga reactotron-react-native reactotron-redux reactotron-redux-saga

You need to make some files to your repository seems like:

N?o foi fornecido texto alternativo para esta imagem

config/ReactotronConfig.ts: here is the reactoton configura??o that we’ll talk about it in another article.

store: The store is the folder where we put all the Redux folder.

modules: Here we’ll put all the redux modules as the auth which will store and manipulate all the auth states.

I’ll explain each file later.

The is an application that we use to know the redux states and in the ReactotronConfig must be:

N?o foi fornecido texto alternativo para esta imagem


In the creatStore.ts we’ll make the functions that will create our store. Putting each thing in a separate file helps to maintain our project more organized.

N?o foi fornecido texto alternativo para esta imagem


The persistReducers.ts is the file that makes our states persist, which means after the user closes the application the states will still persist which means being saved on the user’s cellphone. The persistentReducer needs some parameters as the key that we should put the name of our application, the React will use that name to find our states saved on the user’s cellphone, be careful if you change the key anytime the React won’t find the older states. The storage is what will be used to save the state since we’re using the React native we have to use the AsyncStorage to store and maintain our states using it, we can just worried about the redux as we’ve done on the React js and manipulate every state with no worries, at least we have the blacklist here we can put all module that we don’t wanna save, every module you put there won’t be saved, pay attention the name should have the save module’s name that is on the rootReducer.

N?o foi fornecido texto alternativo para esta imagem

The modules/auth/ducks.ts is our auth module, here we can put all states we want to save on our auth module. Here we’re using the duck's pattern, just to be clear. I won’t go deeper on that because there will be an article about the duck’s pattern.

N?o foi fornecido texto alternativo para esta imagem

Now we can configure the rootReducer and as in the example import the auth, we have to import all the modules one by one.

N?o foi fornecido texto alternativo para esta imagem

Now we can configure and make the penult step to make the Redux works properly store/index.ts should seem.

N?o foi fornecido texto alternativo para esta imagem

At least we can just change our src/index.tsx and have to seem as

N?o foi fornecido texto alternativo para esta imagem

Conclusion

Now you can use the application and make the dispatch normally and manipulate the Redux’s states and it’ll persist normally.






要查看或添加评论,请登录

Guilherme Lisb?a的更多文章

  • Dataform

    Dataform

    Dataform é uma ferramenta de gerenciamento e transforma??o de dados que permite aos analistas construir e manter…

  • How to Use Advanced Techniques in BigQuery to Optimize Your Queries

    How to Use Advanced Techniques in BigQuery to Optimize Your Queries

    BigQuery is a data analytics tool from Google that allows users to perform complex queries on large datasets with ease.…

  • SQL Tips and Tricks

    SQL Tips and Tricks

    Here we'll propose a advantage use cases to improve your query performance and make your query less complex. Common…

  • ETL x ELT

    ETL x ELT

    When we think in how to bring data from data sources and databases into a lakehouse we can use ETL or ELT. Extract :…

  • RDD vs DataFrame vs DataSet

    RDD vs DataFrame vs DataSet

    Talking about Spark there are some things we need to know. The first is about RDD, DataFrame, and DataSet.

  • Ducks Pattern

    Ducks Pattern

    The Ducks Pattern is a way to organize the Redux structure and the Redux is a library of state control based on Flux…

  • Root import

    Root import

    The root import is a tool that helps us to easily import a module from anywhere that is. React Js First of all, you…

  • Por que usar Typescript ?

    Por que usar Typescript ?

    Essa é a pergunta que eu sempre escuto sempre que estou sugerindo que seja realizado usando Typescript. Por que usar…

社区洞察

其他会员也浏览了