1、引入react-redux,redux,? redux-thunk,? redux-persist
? ? ? ? npm install react-redux redux redux-thunk redux-persist,
? ? ? ? 其中redux-thunk是一個中間件說的是可以提供異步派遣action,redux-persist數據持久化存儲
2、項目入口文件index.js
// Provider是react-redux兩個核心工具之一,作用:將store傳遞到每個項目中的組件中
// 第二個工具是connect,稍后會作介紹
import { Provider } from 'react-redux'
// 引入創建好的store實例
import store, { persistor } from './store/index';
import { PersistGate } from 'redux-persist/integration/react';
ReactDOM.render(
? <React.StrictMode>
? ? <Provider store = {store}>
? ? <PersistGate loading={null} persistor={persistor}>
? ? <App />
? ? </PersistGate>
? ? </Provider>
? </React.StrictMode>,
? document.getElementById('root')
);
3、src目錄下創建store文件夾
? ?3.1 store/index.js 文件
// applyMiddleware: redux通過該函數來使用中間件
// createStore: 用于創建store實例
import { applyMiddleware, createStore } from "redux";
// 中間件,作用:如果不使用該中間件,當我們dispatch一個action時,需要給dispatch函數傳入action對象;但如果我們使用了這個中間件,那么就可以傳入一個函數,這個函數接收兩個參數:dispatch和getState。這個dispatch可以在將來的異步請求完成后使用,對于異步action很有用
import thunk from 'redux-thunk'
// 引入reducer
import reducers from './reducers.js'
import { persistStore, persistReducer } from 'redux-persist' // 用來避免刷新導致store重置
import storage from 'redux-persist/lib/storage'; //將redux保存到localstorage中
// import storage from 'redux-persist/lib/storage/session'; //將redux保存到sessionstorage中
const myReducer = persistReducer({
? key: 'root',
? storage
}, reducers)
const store = createStore(
? myReducer,
? applyMiddleware(thunk)
)
export const persistor = persistStore(store);
export default store????
3.2 store/action.js 文件
export function setUserInfo (data) {
? return (dispatch, getState) => {
? ? dispatch({ type: 'SET_USER_INFO', userInfo: data })
? }
}
3.3 store/state.js 文件
export default {
? userInfo: {}
}
3.4 store/reducers.js文件
// 工具函數,用于組織多個reducer,并返回reducer集合
import { combineReducers } from "redux";
// 默認值
import defaultState from "./state";
// 一個reducer就是一個函數
console.log(defaultState.userInfo, '**************')
function userInfoReducers(state = defaultState.userInfo, action) {
? // 不同的action有不同的處理邏輯
? switch (action.type) {
? ? case 'SET_USER_INFO':
? ? ? return action.userInfo
? ? default:
? ? ? return state
? }
}
// 導出所有reducer
export default combineReducers({
? userInfo: userInfoReducers
})
4、src下創建views文件夾用來存放組件
4.1 views文件下創建userInfo.jsx文件
import React from 'react'
import { connect } from "react-redux"
import { setUserInfo } from "../store/actions";
function UserInfo({userInfo, setUserInfo}) {
? const changeUserInfo = () => {
? ? setUserInfo({name: '1', age: 20, sex: '女'})
? }
? return <div>
? ? 我是一個用戶信息組件
? ? <h1>用戶名稱:{userInfo.name}</h1>
? ? <h2>性別:{userInfo.sex}</h2>
? ? <h2>年齡:{userInfo.age}</h2>
? ? <button onClick={changeUserInfo}>修改信息</button>
? </div>
}
// mapStateToProps:將state映射到組件的props中
const mapStateToProps = (state) => {
? console.log(state, '當前的state是什么')
? return {
? ? userInfo: state.userInfo
? }
}
// mapDispatchToProps:將dispatch映射到組件的props中
const mapDispatchToProps = (dispatch) => {
? return {
? ? setUserInfo (data) {
? ? ? dispatch(setUserInfo(data))
? ? }
? }
}
export default connect(mapStateToProps, mapDispatchToProps)(UserInfo);
4.2 在app.js文件中引入userInfo組件
import './App.css';
import UserInfo from "./views/userInfo.jsx";
function App() {
? return (
? ? <div className="App">
? ? ? <UserInfo></UserInfo>
? ? </div>
? );
}
export default App;
具體使用看views/userInfo.jsx中的調用,以上為react-redux簡單使用,整理一下。