- 用於集中管理狀態、不同組件間可互通資料
- 現在比較流行的套件組合為:React Redux + Redux Toolkit
- Redux Toolkit:為一個更有效率撰寫 Redux 的 library
- 重點API:Store、Actions、Reducers
- 新增React Redux 及 Redux Toolkit至Project
npm install @reduxjs/toolkit react-redux
- 建立Redux Store
- 創建
src/store/index.js
- 從Redux Toolkit引入
configureStore
API
import { configureStore } from '@reduxjs/toolkit' export const store = configureStore({ reducer: {}, })
- 此舉目的為,Redux能配置Redux DevTools extension
- 創建
- 將Redux於Project激活
- 於
src/index.js
中,引入Redux的<Provider>
import React from 'react' import ReactDOM from 'react-dom' import './index.css' import App from './App' import { store } from './store/index' import { Provider } from 'react-redux' ReactDOM.render( <Provider store={store}> <App /> </Provider>, document.getElementById('root') )
- 於
- 創建Redux State Slice
- 創建
src/features/colorSlice.js
- 從Redux Toolkit引入
createSlice
API - 創建Slice必要元素
- String name:用於辨識slice
- Initial state value
- One or more reducer functions:定義方法
- Export the
generated Redux action creators
and thereducer function
for the whole slice.
import { createSlice } from '@reduxjs/toolkit' const initialState = { color_index:0, color_list: ["#E09200","#FFA1A1","#C7C400","#009400","#00B2B2","#6E6EFF"], bgColorCode:"#E09200" } export const colorSlice = createSlice({ name: 'color', initialState, reducers: { next: (state) => { if(state.color_index >= 5){ state.color_index = 0; }else{ state.color_index += 1; } state.bgColorCode = state.color_list[state.color_index] }, previous: (state) => { if(state.color_index <= 0){ state.color_index = 5; }else{ state.color_index -= 1; } state.bgColorCode = state.color_list[state.color_index] }, changeByColorCode: (state, action) => { state.bgColorCode = action.payload }, }, }) // Action creators are generated for each case reducer function export const { next, previous, changeByColorCode } = colorSlice.actions export default colorSlice.reducer
- 創建
- 註冊Slice Reducers至Store (src/store/index.js)
import { configureStore } from '@reduxjs/toolkit' import colorReducer from '../features/colorSlice' export const store = configureStore({ reducer: { color: colorReducer, }, })
- 於React Components中使用Redux State and Actions
- 使用
useSelector
read data - 使用
useDispatch
dispatch actions
// src/App.jsx import { useState } from 'react' import "./App.css" import { useSelector, useDispatch } from 'react-redux' import { next, previous, changeByColorCode } from './features/colorSlice' export default function App() { const bgColor = useSelector((state) => state.color.bgColorCode) const dispatch = useDispatch() const [inputColor, setInputColor] = useState(bgColor); return ( <div className="App"> <header className='App-header' style={{ backgroundColor: bgColor}}> <button onClick={() => dispatch(previous())} > Previous </button> <button onClick={() => dispatch(next())} > Next </button> <hr/> <input type="text" value={inputColor} onChange={(event) => setInputColor(event.target.value)}></input> <button onClick={() => dispatch(changeByColorCode(inputColor))} > ChangeColor </button> </header> </div> ) }
- 使用
git clone https://github.com/hanc1027-articles/react-redux-init.git
npm install
npm run start