React Native using Redux - An Intro
Tags: reactnative, expo
Published: 2020-04-12 18:35:32

This article introduces the concept of Actions, Reducers, Store.

####**Actions**
Actions are payloads of information that send data from your application to your store. They are the only source of information for the store. You send them to the store using <code>store.dispatch().</code>

**Type**:- Actions are plain JavaScript objects. Actions must have a type of property that indicates the type of action being performed. Types should typically be defined as string constants. Once your app is large enough, you may want to move them into a separate module.

**Action type**: - 
Here's an example action which represents request the client info:
<code>export const REQUEST_CLIENT = 'REQUEST_CLIENT';</code>

**Action creator**:- function which return actions.
Example, where API has been called to fetch the client's list.
```javascript
export function requestClient() {
    return {
       type: REQUEST_CLIENT,
      }
    }

 export function fetchClientWithId(id) {
 return dispatch => {
    dispatch(requestClient())
  var clientList = store.getState().clients.clientList;
    return fetchAPI('GET', 'clients/{0}'.format(id)).then((json) => 
    {
        if (json != null) {
        dispatch(receiveClient([json]))
        return json
        }
    })
 }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nqdh10kw4udn3i5zuhd3.png)

####**Reducers**

* A *reducer* (also called a *reducing function*) is a function that accepts an accumulation and a value and returns a new accumulation. They are used to reduce a collection of values down to a single value
This is a reducer, a pure function with (state, action) => state signature. * It describes how an action transforms the state into the next state. 
Reducers specify how the application's state changes in response to actions sent to the store.
<code>Remember that actions only describe what happened, but don't describe how the application's state changes.</code>

Ex:-
```javascript
function
counter(state = 0, action) 
{ 
 switch (action.type) 
  { 
    case 'INCREMENT’:
       return state + 1 
   case 'DECREMENT’: 
     return state - 1 default: return state 
  }
}
```


Create the new state:- <code> var newState = Object.assign({}, state, {}); </code>

Use of Reducer composition:- It's important to note that you'll only have a single store in a Redux application. When you want to split your data handling logic, you'll use reducer composition instead of many stores.

<code>Do not put API calls into reducers.*</code>

<code>export type Reducer<S> = (state: S, action: any action) => S; (refer the index.d.ts in redux file in node_modules folder)</code>

####**Store**

In the previous sections, we defined the actions that represent the facts about “what happened” and the reducers that update the state according to those actions.
The Store is the object that brings them together. The store has the following responsibilities:
- Holds application state;
- Allows access to state via <code>getState();</code>
- Allows state to be updated via <code>dispatch(action);</code>
- Registers listeners via <code>subscribe(listener);</code>
- Handles the unregistering of listeners via the function returned by <code>subscribe(listener).</code>


[Sample project link](https://redux.js.org/advanced/example-reddit-api)