我有一个包装器,用于初始化redux存储,并在将其作为道具传递给孩子之前,等待api模块的构建。 AppContainer
function AppContainer(props) {
const { apiConnector, initialState } = props;
const [fetched, setFetched] = useState(false);
const [store, setStore] = useState(createStore(initialState));
useEffect(() => {
if (apiConnector) {
setFetched(true);
setStore(createStore(initialState, { apiConnector }))
}
}, [apiConnector]);
console.log('AppContainer', fetched)
return (
<Provider store={store}>
<ConnectedLocaleProvider>
<BrowserRouter basename={getBasename()}>
<Routes store={store} loading={!fetched} />
</BrowserRouter>
</ConnectedLocaleProvider>
</Provider>
);
}
export default ConnectorLoadWrapper(AppContainer);
如您所见,我已经构建了在构建apiConnector时触发的效果挂钩。这样可以成功将获取的状态更新为true
,但是由于某些原因,它不会传播到<Routes />
export default function Routes(props) {
const { store, loading } = props;
console.log('Routes', loading)
if (loading) {
return <Loading />;
}
return (
{...myRoutes()}
);
}
请注意每个文件中的控制台日志语句。