React Hooks 在 2018 年年末就已經公佈了,正式發佈是在 2019 年 5 月,關於它到底能作什麼用,並不在本文的探討範圍以內,本文旨在摸索,如何基於 Hooks 以及 Context,實現多組件的狀態共享,完成一個精簡版的 Redux。css
yarn create create-app hooks-context-based-state-management-react-app cd hooks-context-based-state-management-react-app yarn start
或者能夠直接 clone
本文完成的項目:react
git clone https://github.com/pantao/hooks-context-based-state-management-react-app.git
絕大多數狀況下,咱們其實只須要共享會話狀態便可,在本文的示例中,咱們也就只共享這個,在 src
目錄下,建立一個 store/types.js
文件,它定義咱們的 action 類型:git
// 設置 session const SET_SESSION = "SET_TOKEN"; // 銷燬會話 const DESTROY_SESSION = "DESTROY_SESSION"; export { SET_SESSION, DESTROY_SESSION }; export default { SET_SESSION, DESTROY_SESSION };
接着定義咱們的 src/reducers.js
:github
import { SET_SESSION, DESTROY_SESSION } from "./types"; const initialState = { // 會話信息 session: { // J.W.T Token token: "", // 用戶信息 user: null, // 過時時間 expireTime: null } }; const reducer = (state = initialState, action) => { console.log({ oldState: state, ...action }); const { type, payload } = action; switch (type) { case SET_SESSION: return { ...state, session: { ...state.session, ...payload } }; case DESTROY_SESSION: return { ...state, session: { ...initialState } }; default: throw new Error("Unexpected action"); } }; export { initialState, reducer };
src/actions.js
import { SET_SESSION, DESTROY_SESSION } from "./types"; export const useActions = (state, dispatch) => { return { login: async (username, password) => { console.log(`login with ${username} & ${password}`); const session = await new Promise(resolve => { // 模擬接口請求費事 1 秒 setTimeout( () => resolve({ token: "J.W.T", expireTime: new Date("2030-09-09"), user: { username, password } }), 1000 ); }); // dispatch SET_TOKEN dispatch({ type: SET_SESSION, payload: session }); return session; }, logout: () => { dispatch({ type: DESTROY_SESSION }); } }; };
store/StoreContext.js
import React, { createContext, useReducer, useEffect } from "react"; import { reducer, initialState } from "./reducers"; import { useActions } from "./actions"; const StoreContext = createContext(initialState); function StoreProvider({ children }) { // 設置 reducer,獲得 `dispatch` 方法以及 `state` const [state, dispatch] = useReducer(reducer, initialState); // 生成 `actions` 對象 const actions = useActions(state, dispatch); // 打印出新的 `state` useEffect(() => { console.log({ newState: state }); }, [state]); // 渲染 state, dispatch 以及 actions return ( <StoreContext.Provider value={{ state, dispatch, actions }}> {children} </StoreContext.Provider> ); } export { StoreContext, StoreProvider };
src/index.js
打開 src/index.js
:redux
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import * as serviceWorker from './serviceWorker'; ReactDOM.render(<App />, document.getElementById('root')); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. // Learn more about service workers: https://bit.ly/CRA-PWA serviceWorker.unregister();
作以下修改:session
import React from "react"; import ReactDOM from "react-dom"; import "./index.css"; import App from "./App"; import * as serviceWorker from "./serviceWorker"; import { StoreProvider } from "./context/StoreContext"; // 導入 StoreProvider 組件 ReactDOM.render( <StoreProvider> <App /> </StoreProvider>, document.getElementById("root") ); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. // Learn more about service workers: https://bit.ly/CRA-PWA serviceWorker.unregister();
src/App.js
內容以下:app
import React, { useContext, useState } from "react"; import logo from "./logo.svg"; import "./App.css"; import { StoreContext } from "./store/StoreContext"; import { DESTROY_SESSION } from "./store/types"; function App() { const { state, dispatch, actions } = useContext(StoreContext); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const { user, expireTime } = state.session; const login = async () => { if (!username) { return alert("請輸入用戶名"); } if (!password) { return alert("請輸入密碼"); } setLoading(true); try { await actions.login(username, password); setLoading(false); alert("登陸成功"); } catch (error) { setLoading(false); alert(`登陸失敗:${error.message}`); } }; const logout = () => { dispatch({ type: DESTROY_SESSION }); }; return ( <div className="App"> <header className="App-header"> <img src={logo} className="App-logo" alt="logo" /> {loading ? <div className="loading">登陸中……</div> : null} {user ? ( <div className="user"> <div className="field">用戶名:{user.username}</div> <div className="field">過時時間:{`${expireTime}`}</div> <div className="button" onClick={actions.logout}> 使用 actions.logout 退出登陸 </div> <div className="button" onClick={logout}> 使用 dispatch 退出登陸 </div> </div> ) : ( <div className="form"> <label className="field"> 用戶名: <input value={username} onChange={e => setUsername(e.target.value)} /> </label> <label className="field"> 密碼: <input value={password} onChange={e => setPassword(e.target.value)} type="password" /> </label> <div className="button" onClick={login}> 登陸 </div> </div> )} </header> </div> ); } export default App;
整個實現咱們使用到了 React
的 useContext
共享上下文關係,這個是關係、useEffect
用來實現 reducer
的 log
,useReducer
實現 redux
裏面的 combineReducer
功能,總體上來說,實現仍是足夠絕大多數中小型項目使用的。dom