关于数据流的实践记录

 
设计一个shopping Cart的、数据流
import React, { createContext, useContext, useReducer } from 'react'; /** * 购物车数据list */ export interface IShopping { addTime: number; duration: number; encryptId: string; format: string; materialId: string; mediaType: string; offset: number; plat: string; previewUrl: string; source: number; thumbnail: string; title: string; endTime?: number; startTime?: number; } interface IState { shoppingList: IShopping[]; } export interface updateShoppingCarAction { type: 'update'; data: IShopping[]; } export type shoppingAction = updateShoppingCarAction; interface IStoreContext { shoppingState: IState; dispatch: React.Dispatch<shoppingAction>; } const initialState: IState = { shoppingList: [] }; const shoppingContext = createContext({} as IStoreContext); const { Provider } = shoppingContext; const ShoppingStateProvider = ({ children }: { children: JSX.Element }) => { const reducer = (state: IState, action: shoppingAction) => { switch (action.type) { case 'update': return { ...state, shoppingList: action.data }; default: return state; } }; const [shoppingState, dispatch] = useReducer(reducer, initialState); return <Provider value={{ shoppingState, dispatch }}>{children}</Provider>; }; // function function useShopping() { const context = useContext(shoppingContext); if (context === undefined) { throw new Error('useShopping must be used within a ShoppingProvider'); } return context; } // class function ShoppingConsumer({ children }: { children: any }) { return ( <shoppingContext.Consumer> {(context) => { if (context === undefined) { throw new Error( 'ShoppingConsumer must be used within a ShoppingProvider' ); } return children(context); }} </shoppingContext.Consumer> ); } export { ShoppingStateProvider, useShopping };
 
以前都是使用Redux来全中心化管理数据流,现在用context将redux的中心化store拆分成多个store,如果熟悉redux上面的代码应该会很熟悉,其实原理是一样的。同样是store,action, reducer的结构,把context的provider包裹一层。将state和action通过context的value暴露出去。
然后写一个useShopping,不需要反复调用useContext传入Context的store,实现随取随用。