[inulax] 状态管理器dispatch方法异步使用时数据无法更新
This commit is contained in:
parent
3019fa5581
commit
0f87229318
|
@ -41,29 +41,27 @@ export function createStoreHook(context: Context): () => ReduxStoreHandler {
|
|||
};
|
||||
}
|
||||
|
||||
export function createSelectorHook(context: Context): (selector?: (any) => any) => any {
|
||||
const store = createStoreHook(context)() as unknown as ReduxStoreHandler;
|
||||
return function (selector = state => state) {
|
||||
const [b, fr] = useState(false);
|
||||
export function createSelectorHook(context: Context): (selector?: ((state: unknown) => any) | undefined) => any {
|
||||
const store = createStoreHook(context)();
|
||||
return function useSelector(selector = state => state) {
|
||||
const [state, setState] = useState(() => store.getState());
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = store.subscribe(() => fr(!b));
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
const unsubscribe = store.subscribe(() => {
|
||||
setState(store.getState());
|
||||
});
|
||||
return () => unsubscribe();
|
||||
}, []);
|
||||
|
||||
return selector(store.getState());
|
||||
return selector(state);
|
||||
};
|
||||
}
|
||||
|
||||
export function createDispatchHook(context: Context): () => BoundActionCreator {
|
||||
const store = createStoreHook(context)() as unknown as ReduxStoreHandler;
|
||||
return function () {
|
||||
return action => {
|
||||
store.dispatch(action);
|
||||
const store = createStoreHook(context)();
|
||||
return function useDispatch() {
|
||||
return store.dispatch;
|
||||
};
|
||||
}.bind(store);
|
||||
}
|
||||
|
||||
export const useSelector = selector => {
|
||||
|
|
Loading…
Reference in New Issue