DEV Community

Cover image for React State Management (2) : Redux
Yuko
Yuko

Posted on • Edited on

React State Management (2) : Redux

This is a series of memos referring to the ways of React state management: context API, Redux, Redux toolkit and Recoil. The topic in this article is Redux.

The chart below is the whole image of this practice application. ComponentA accepts user input text and passes it over to ComponentB as a prop. At the same time, dispatch the action to save the data in the store so that ComponentC and componentD can use it.


Redux Fundamentals, Part 1: Redux Overview | Redux

This is the image of this application.

This is the structure of files in src folder.

1) Set up types, actions, reducers, and store

First of all, you need to install redux and react-redux.

npm install redux react-redux
Enter fullscreen mode Exit fullscreen mode

types

    export const SUBMIT = "SUBMIT";
Enter fullscreen mode Exit fullscreen mode

actions

    import { SUBMIT } from "./types";

    export const submit = (text) => ({
      type: SUBMIT,
      payload: text,
    });
Enter fullscreen mode Exit fullscreen mode

reducer

    import { SUBMIT } from "./types";
    const INIT_STATE = {
      text: null,
    };

    const reducer = (state = INIT_STATE, action) => {
      if (action.type === SUBMIT) {
        return {
          text: action.payload,
        };
      } else {
        return state; //provide the default action to return state which redux uses when initialization
      }
    };

    export default reducer;
Enter fullscreen mode Exit fullscreen mode

store

    import { createStore } from "redux";
    import reducer from "./reducer";

    const store = createStore(reducer);

    export default store;
Enter fullscreen mode Exit fullscreen mode

2) Provider

index.js

    import React from "react";
    import ReactDOM from "react-dom";
    import "./index.css";
    import App from "./App";

    import { Provider } from "react-redux";
    import store from "./redux/store";

    ReactDOM.render(
      <Provider store={store}>
        <App />
      </Provider>,
      document.getElementById("root")
    );
Enter fullscreen mode Exit fullscreen mode

3) useDispatch, useSelector

ComponentA

    import { useState } from "react";
    import { useDispatch } from "react-redux";
    import { submit } from "../redux/actions";
    import ComponentB from "./ComponentB";
    const ComponentA = () => {
      const [value, setValue] = useState("");
      const dispatch = useDispatch();
      const changeHandler = (e) => {
        setValue(e.target.value);
        dispatch(submit(e.target.value));
      };
      return (
        <>
          <input type="text" value={value} onChange={changeHandler} />
          <ComponentB text={value} />
        </>
      );
    };

    export default ComponentA;
Enter fullscreen mode Exit fullscreen mode

ComponentC

    import { useSelector } from "react-redux";
    const ComponentC = () => {
      const text = useSelector((state) => state.text);
      return (
        <>
          <h1>Uppercase</h1>
          <h2>{text && text.toUpperCase()}</h2>
        </>
      );
    };
    export default ComponentC;
Enter fullscreen mode Exit fullscreen mode

ComponentD

    import { useSelector } from "react-redux";
    const ComponentD = () => {
      const text = useSelector((state) => state.text);
      return (
        <>
          <h1>Lowercase</h1>
          <h2>{text && text.toLowerCase()}</h2>
        </>
      );
    };

    export default ComponentD;
Enter fullscreen mode Exit fullscreen mode

The whole code is available here

Please read them as well. These are more simple ways than normal Redux to get the same result :)
React State Management (1) : context API
React State Management (3) : Redux Toolkit

Thank you for reading :)

The original article is here

Top comments (0)