Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a difference between action,reducer and store in redux?

I am new to react/redux. I am trying to figure out how all the pieces in redux interact. The one thing giving me trouble is understanding the relation between actions and reducers,store.

like image 233
Dishant Chanchad Avatar asked Jan 27 '19 05:01

Dishant Chanchad


People also ask

What is the difference between action and reducer in Redux?

Reducers: As we already know, actions only tell what to do, but they don't tell how to do, so reducers are the pure functions that take the current state and action and return the new state and tell the store how to do.

What is a store in Redux?

A store is an immutable object tree in Redux. A store is a state container which holds the application's state. Redux can have only a single store in your application. Whenever a store is created in Redux, you need to specify the reducer.

What are actions in Redux?

Actions are the only source of information for the store as per Redux official documentation. It carries a payload of information from your application to store. As discussed earlier, actions are plain JavaScript object that must have a type attribute to indicate the type of action performed.

What is the main purpose of a reducer function in Redux?

Introduction. In Redux, a reducer is a pure function that takes an action and the previous state of the application and returns the new state. The action describes what happened and it is the reducer's job to return the new state based on that action.


1 Answers

It's pretty simple when you think about it:

  • Store - Is what holds all the data your application uses.
  • Reducer - is what manipulates that data when it recieves an action.
  • Action - is what tells reducer to manipulate the store data, it carries the name and (not required) some data.

Reducer is usually in a format of a switch statement, that switches between all possible Actions (Cases) and then manipulates the Store data based on action. When a reducer data changes within the redux, the properties in your components are changed and then the re-render ocurrs.

like image 192
Desomph Avatar answered Sep 30 '22 19:09

Desomph