# yushidux **Repository Path**: yushi5344/yushidux ## Basic Information - **Project Name**: yushidux - **Description**: redux组件的简单实现 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-02-24 - **Last Updated**: 2022-05-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # yushidux 1. 组件状态的集中管理库,用法类似于`redux` 2. 创建reducers ```javascript const initState = { type: 'receive', date: dateFormat("yyyy-MM-dd", new Date()) } export const searchRecucer = (action,preState = initState) => { let {type, data} = action; switch (type) { case 'GOSEARCH': return data; default: return preState; } } const initTest={ a:1,b:2 } export const testReducer=(action,preState=initTest)=>{ let {type, data} = action; switch (type) { case 'GOTEST': return data; default: return preState; } } ``` 3. 创建actions ```javascript export const search = (data) => { return {type: "GOSEARCH", data} } const change = (data) => { return {type: "GOTEST", data}; } // 异步action export const changeTest = (data) => { return (dispatch,preState)=> { setTimeout(() => { dispatch(search(data)); }, 2000); } } ``` 4. 创建store ```javascript import {createStore,combineReducers} from "yushidux"; import {searchRecucer,testReducer} from "./reducers"; const reducers={ search:searchRecucer, test:testReducer }; export default createStore(combineReducers(reducers)) ``` 5. 在修改store数据 ```javascript import store from "../../store"; import {search} from "../../store/actions"; // store.dispatch(search(data)) ``` 6. 在组件中订阅数据变化 ```javascript store.subscribe(()=>{ // dosomething }) ```