Adding Redux to your React Native App
Published: 2020-07-28 15:21:47

---

firstPublishedAt: 1595850022606
latestPublishedAt: 1595871328841
slug: adding-redux-to-your-react-native-app
title: Adding Redux to your React Native App

---

![](https://cdn-images-1.medium.com/max/2000/1*rn81KthOlgxpSJZ1RHVtTQ.png)

> **_Why Do We Need Global State ?_**

If we want to pass a data to a component from another one, we have few options. If they have a relation then it’s easy, we just pass the data as a prop. But as the complexity grows it is harder to track down the component tree and props. What if the components don’t have a relation? That’s where global state comes into play. Imagine Redux global store as a component at higher level than all of your componets and you can pass and fetch any data from there via actions and reducers at any level. In this article we will show you how to add and use Redux in your React Native App.

> **_How To Install React-Redux_**

Open your project folder in your terminal and run commands below.

```
npm install redux react-redux redux-persist @react-native-community/async-storage --save
```

---

> **_What Are Reducers and Actions ?_**

A **reducer** is a function that determines changes to an application’s state. It uses the **action** it receives to determine this change.

As you go deep in Redux, you’ll find out that all you’re going to do will be in reducers and actions. If you can grasp these concepts well, you’ll master redux in no time. Let’s start.

---

> **_Write Your First Reducer_**

Let’s create a folder named **redux **in project level and inside that two more folders named **actions **and **reducers. **In reducers folder create a file. Let’s name it **userReducer**. We’ll use this reducer to pass down a string(name) and a boolean(loggedIn) value.

![](https://cdn-images-1.medium.com/max/1016/1*Lie38cd_NrOkzQkJvkLkIg.png)

At the top we will define an Initial state for default values. After that we’ll define our reducer.

Our reducer will have two parameters. First one is our state and the second one is the action that triggers our state changes. We change our state based on the type of action.

> **_Write Your First Action_**

In actions folder create a file named **userActions**. We have two actions, first one is for passing down the username to the reducer and second one is for resetting the reducer.

![](https://cdn-images-1.medium.com/max/664/1*nimBBMZs9moh6y0-fh13ww.png)

> **_Root Reducer_**

If you have more than one reducer in your app then you need to create a root reducer to combine all your reducers. In reducers folder create another file named rootReducer.

![](https://cdn-images-1.medium.com/max/658/1*PUncXiNxzVeUnqcFHCb2Hw.png)

_Note that when you export, left side of the object is how you will call the reducer state in components._

---

> **_What is Redux-Persist ?_**

If you want to keep your data in redux even if you close your app you need redux-persist. In redux-persist you can decide which data you’ll keep and which data you’ll lose.

In redux folder create a file named store.

![](https://cdn-images-1.medium.com/max/1102/1*ZnrWFG-Lv71etGdR6bCPfA.png)

Now it’s time to wrap our app with the store we created.

Let’s open our App.js file. To access redux data from all components we should wrap our app with Provider and give store as a prop.

And for redux-persist, at a level below we wrap our app with PersistGate and give persistor as prop and set loading prop as null.

![](https://cdn-images-1.medium.com/max/1048/1*ei4LCH07VA2DQILbCKu38g.png)

Now we’re ready to use our reducers and actions in our app.

So let’s use them.

---

> **_What are MapStateToProps and MapDispachToProps ?_**

> **_How To Send Data To Redux Store?_**

In this example we have a text input and a button for user to type their name and proceed with their name. We have a component called landingPage. In this file before we use actions we have to import them.

![](https://cdn-images-1.medium.com/max/950/1*TNLHhznUTviQT_coCC7ctg.png)

For triggering the action we need to use **mapDispatchToProps **function and to get the data from redux store we need to use **mapStateToProps **function.

![](https://cdn-images-1.medium.com/max/662/1*K9EmUqdZ39f18LrjqTJE-g.png)

**Dispatch **is a function that will trigger our actions. We pass a parameter to the reducer.The reducer will use that parameter once the action is triggered.

![](https://cdn-images-1.medium.com/max/1082/1*p1RSo3V0ChZKTFZZkz_NCg.png)

Using action is quite easy. We just need to define it from props and use it as a function. As you can see in the image above we pass the username state as parameter and call action. This will trigger our action and go to the reducer with the username data.

> **_How To Receive Data From Redux Store?_**

We have a second page to show user name in another component.

For getting the data from redux store we need to use **mapStateToProps **function. We will get user from state of our store.

And for logout we also define resetUserAction.

![](https://cdn-images-1.medium.com/max/672/1*2b7el4g5TOBfNA4wl_S04w.png)

![](https://cdn-images-1.medium.com/max/1054/1*e2pm9XGYdyHZXlBR8lAEcw.png)

Using data from redux store is also quite easy. Like action; just define it from props and use it wherever you want.

![](https://cdn-images-1.medium.com/max/600/1*LkV08Jil39VuLr9sN8D-6g.gif)

---

> **_Conclusion:_**

We tried to show and explain how to use Redux in React-Native. Redux may take some time to learn but with practice and time you will master your skills. For more information use can check the Redux documents [here](https://redux.js.org/introduction/getting-started).

You can clone this project from [**here **](https://github.com/mtateam/react-native-examples)via github**.**

---

**MTA Team:**

[Ali Gümüş Tosun](https://medium.com/u/780987e259b0?source=post_page-----6e15f3fbf9e1----------------------) - [Semih Sevincli](https://medium.com/u/f07974fc764c?source=post_page-----6e15f3fbf9e1----------------------) - [Aslancan Yılmaz](https://medium.com/u/fca2225fd420?source=post_page-----6e15f3fbf9e1----------------------) - [Burak Arıcı](https://medium.com/u/e86295cf2fe8?source=post_page-----6e15f3fbf9e1----------------------)

**LinkedIn Accounts:**

[Ali Gümüş Tosun](https://www.linkedin.com/in/ali-g%C3%BCm%C3%BC%C5%9F-tosun-9a6736129/) - [Semih Sevinçli](https://www.linkedin.com/in/semih-sevin%C3%A7li-02a0a4ba/) - [Aslancan Yılmaz](https://www.linkedin.com/in/aslancan-y%C4%B1lmaz-002965131/) - [Burak Arıcı](https://www.linkedin.com/in/mustafa-burak-ar%C4%B1c%C4%B1-40889618b/)