在响应dom之前确保调度和减速器工作

时间:2019-02-13 10:21:36

标签: reactjs redux react-redux react-dom

Api.GET({
    url: USER_LOGIN_AUTH,
    payload: null,
    onSuccess: response => {
        window.__MUI_USE_NEXT_TYPOGRAPHY_VARIANTS__ = true;
        store.dispatch(getUserAction(response.data));
        ReactDOM.render(
            <Provider store={store}>
                <BrowserRouter>
                    <App />
                </BrowserRouter>
            </Provider>,
            document.getElementById("root")
        );
    },
    onFailure: () => {
        ReactDOM.render(<Unauthorized />);
    }
});

如何确保在行store.dispatch执行之前,ReactDOM.render及其相应的化简器已设置了新状态。

我希望在设置初始状态之前不渲染我的应用。

谢谢

1 个答案:

答案 0 :(得分:0)

使用Promise可以帮助您

Api.GET({
    url: USER_LOGIN_AUTH,
    payload: null,
onSuccess: response => {
    window.__MUI_USE_NEXT_TYPOGRAPHY_VARIANTS__ = true;

    new Promise(function(resolve, reject) {

        try {
            resolve(store.dispatch(getUserAction(response.data)));
        } catch (error) {
            reject(error);
        }

    }).then(store => {

        ReactDOM.render( < Provider store = {store} >
            <BrowserRouter >
            <App />
            </BrowserRouter> </Provider>,
            document.getElementById("root")
        );

    })

},
onFailure: () => {
    ReactDOM.render( < Unauthorized / > );
}
});