Use Redux To Store Data#
Introduction#
Currently we have the state of the FAQ list in the App
component and pass all handlers and data down to the FaqItem
component.
When your application will contain more subcomponents, this can become very complex.
To manage your application state, we will introduce Redux here.
Redux is a state management system which is composed of a store.
This store contains data, a set of reducers which handle (part of) this state and its changes, and actions which are used to trigger state changes.
A reducer is pure function which takes the previous state and an action, and returns a new state based on the data of the action. The new state is then saved to the store. Components can then read data from the store and render a view. When a change needs to be made to the application state, the view will fire an action which will be handled by the reducer again, and so on. This is a unidirectional flow.
Installing#
To install Redux, we will run the following command:
yarn add redux react-redux
Actions#
We will start by creating actions.
We will create a file actions/index.js
with the addFaqItem
action:
1export const addFaqItem = (question, answer) => ({
2 type: "ADD_FAQ_ITEM",
3 question,
4 answer
5});
Write the editFaqItem
and deleteFaqItem
actions.
Reducers#
Next we will create the reducer by creating the reducers/faq.js
file.
As stated earlier, a reducer is a pure function which takes the previous state and an action, and returns the new state.
It will look like this:
1const faq = (state = [], action) => {
2 // Do something
3};
4
5export default faq;
Finish the reducer so that it can handle the ADD_FAQ_ITEM
, EDIT_FAQ_ITEM
, and DELETE_FAQ_ITEM
actions.
Combine Multiple Reducers#
When our application grows, we will have multiple reducers handling a specific part of the data.
We will combine all reducers into one index reducer, such that we can set all reducers in one store.
We will create the file reducers/index.js
.
1import { combineReducers } from "redux";
2import faq from "./faq";
3
4export default combineReducers({
5 faq
6});