Answer the question
In order to leave comments, you need to log in
Where to get initState in Redux in my case?
I store information about the user in the Redux state, the first time I visit the site, a request will be sent to the server for information about the user. I understand that it's easier to do in DidMount in the root component, but how do I do it in my case?
My index.js
ReactDOM.render(
<Provider store={store}>
<Router history={history}>{routes}</Router>
</Provider>,
document.getElementById("root")
);
const routes = (
<Switch>
<PrivateRoute exact path="/messages" component={MessagesPageContainer} />
<RestrictedRoute exact path="/login" component={LoginPageContainer} />
<RestrictedRoute exact path="/register" component={RegisterPageContainer} />
<Route component={NoMatch} />
</Switch>
);
export default routes;
export const onInitUser = () => dispatch => {
const capi = new UserApi();
const res = capi.getUser();
res.then(user => {
dispatch({
type: userActions.FETCH_INIT_USER,
data: { user: user }
});
});
};
Answer the question
In order to leave comments, you need to log in
I usually do this:
store.dispatch(clientInit());
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>,
document.getElementById('root'),
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question