【2022最新】React項(xiàng)目實(shí)戰(zhàn)(React后臺(tái)管理系統(tǒng)、TypeScript

這樣封裝也很方便的實(shí)現(xiàn)了,不清楚有沒(méi)有弊端

let stateData = {};
let actionData = {};
let globModules = import.meta.globEager('./modules/*.ts');
Object.entries(globModules).forEach((item: any) => {
? let mod = item[1].default;
? stateData = { ...stateData, ...mod.state };
? actionData = { ...actionData, ...mod.actions };
});
let defaultState = stateData;
// 創(chuàng)建數(shù)據(jù)倉(cāng)庫(kù)
let reducer = (state = defaultState, action: any) => {
? let newState = JSON.parse(JSON.stringify(state));
? if (actionData[action.type]) {
? ? actionData[action.type](newState, action);
? }
? return newState;
};
export default reducer;
標(biāo)簽: