Redux
react global state management library
Redux
Flux pattern
Installation
yarn add redux react-redux redux-devtools-extension @reduxjs/toolkitReducer definition
// libraries import { applyMiddleware, combineReducers, compose, createStore } from 'redux'; import { composeWithDevTools } from 'redux-devtools-extension'; // customs import branchManagementReducer from 'modules/branch/reducer/branchManagement.reducer'; const rootReducer = combineReducers({ branchManage: branchManagementReducer, }); const middleware = applyMiddleware(); const enhancer = process.env.NEXT_PUBLIC_ENV_NODE === 'production' ? compose(middleware) : composeWithDevTools(middleware); export const store = createStore(rootReducer, enhancer);// store/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 }; 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; } }import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import { Provider } from 'react-redux'; import store from 'store'; // import './excercise'; ReactDOM.render( <React.StrictMode> <Provider store={store}> <App /> </Provider> </React.StrictMode>, document.getElementById('root') ); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals reportWebVitals();
Installation
Configure
redux-toolkit
Last updated