我正在构建一个应用程序,在这个应用程序中,我需要在我的主组件挂载之前从服务器获取初始的redux状态。 在我的actions中,我使用redux-thunk来获取必要的数据:
export const getInitialStateSuccess = (state: any): IfetchThemeSuccess => {
console.log('state', state);
return {
type: FETCH_INIT_STATE,
state
};
};
// here I fetch the data from the server and call getInitialStateSuccess
// to pass it to redux init state
export const getInitialState = () => {
return (dispatch: any) => {
api.getInitState().then((data) => {
dispatch(getInitialStateSuccess(data));
});
};
}
然而,我不知道应该在哪里调用getInitialState()。我需要在主组件挂载之前初始化应用程序的状态。您认为在componentWillMount中调用此操作是否是一种好的做法?它只需要被调用一次。 请告诉我上述方法是否是一个好的实践。 谢谢!
{loaded: false, fetching: false, listOfItems: []}
,然后从那里开始工作。 - Nick