[react]redixを使用してカウンタモジュールを作成する


reduceモジュールは、以下のすべての項目を含むJavaScriptファイルを表します.
  • 動作タイプ
  • 動作作成関数
  • 李杜瑟
  • まずreduxプリファレンスパラメータから始めましょう
    npx create-react-app redux-project
    cd redux-project
    yarn add redux
    yarn add react-redux
    module/counter.js
    const SET_DIFF = "counter/SET_DIFF";
    const INCREASE = "counter/INCREASE";
    const DECREASE = "counter/DECREASE";
    
    export const setDiff = diff => ({type: SET_DIFF, diff})
    export const increase = () =>({type:INCREASE})
    export const decrease = () =>({type:DECREASE})
    
    const initialState = {
        number : 0,
        diff : 1 
    }
    
    /*reducer 선언*/
    export default function counter(state=initialState, action){
        switch(action.type){
            case SET_DIFF:
                return{
                    ...state,
                    diff:action.diff
                };
            case INCREASE:
                return{
                    ...state,
                    number: state.number + state.diff
                }
            case DECREASE:
                return{
                    ...state,
                    number: state.number - state.diff
                }
            default:
                return state;
        }
    }
    module/index.js
    import { combineReducers } from "redux";
    import counter from './counter';
    import todos from "./todos";
    
    const rootReducer = combineReducers({
        counter,
    });
    
    export default rootReducer;
    components/Counter.js
    import React from "react";
    
    function Counter({ number, diff, onIncrease, onDecrease, onSetDiff }) {
        const onChange = e => {
            // e.target.value 의 타입은 문자열이기 때문에 숫자로 변환해주어야 합니다.
            onSetDiff(parseInt(e.target.value, 10));
        };
        return (
            <div>
                <h1>{number}</h1>
                <div>
                    <input type="number" value={diff} min="1" onChange={onChange} />
                    <button onClick={onIncrease}>+</button>
                    <button onClick={onDecrease}>-</button>
                </div>
            </div>
        );
    }
    
    
    export default Counter;
    containers/CounterContainer.js
    import React from "react";
    import { useSelector, useDispatch } from "react-redux";
    import Counter from "../components/Counter";
    import { increase, decrease, setDiff } from "../modules/counter";
    
    function CounterContainer() {
        const { number, diff } = useSelector(state => ({
            number: state.counter.number,
            diff: state.counter.diff
        }));
    
        const dispatch = useDispatch();
    
        const onIncrease = () => dispatch(increase());
        const onDecrease = () => dispatch(decrease());
        const onSetDiff = diff => dispatch(setDiff(diff));
    
        return (
            <Counter
                // 상태와
                number={number}
                diff={diff}
                // 액션을 디스패치 하는 함수들을 props로 넣어줍니다.
                onIncrease={onIncrease}
                onDecrease={onDecrease}
                onSetDiff={onSetDiff}
            />
        );
    }
    
    export default CounterContainer;
    app.js
    import './App.css';
    import CounterContainer from './containers/CounterContainer';
    
    function App() {
      return (
        <div>
          <CounterContainer/>
        </div>
      );
    }
    
    export default App;
    index.js
    import React from 'react';
    import ReactDOM from 'react-dom';
    import './index.css';
    import App from './App';
    import {createStore} from 'redux';
    import rootReducer from './modules';
    import {Provider} from 'react-redux';
    
    
    const store = createStore(rootReducer);
    console.log(store.getState());
    
    ReactDOM.render(
      <Provider store={store}>
        <App />
      </Provider>,
      document.getElementById('root')
    );
    ソース:https://react.vlpt.us/redux/05-counter.html