上一篇文章中,提到状态共享问题
框架选型
Redux
直接抛弃,项目不够大,太重了。
目前的解决方案
找了一圈,做了众多对比,最后选了hox
hox
足够轻量,使用简单,没有引入任何新的语法,一切都是hooks,
来看看怎么简单把
import { createModel } from 'hox';
export const useGlobal = createModel(() => {
const [ state, setState ] = useState({ init: false });
const initialize = useCallback(async (payload) => {
setState(preState=>({...preState,...payload}))
}, []);
return [ state,initialize ];
});
使用的时候直接用useGlobal就可以了
搞定状态共享