【发布时间】:2018-02-21 16:36:34
【问题描述】:
我正在构建一个应用程序,我需要在安装主要组件之前从服务器获取初始 redux 状态。 在我的操作中,我使用了 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 中调用此操作是一个好习惯吗?它只能被调用一次。 请让我知道我上面描述的方法是否是一种好的做法。 谢谢!
【问题讨论】:
标签: reactjs redux react-redux