W
W
WizardW2019-07-09 16:31:44
React
WizardW, 2019-07-09 16:31:44

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")
);

All routes.js routes (in fact, this is the 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;

There is an action to get data about users:
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

1 answer(s)
A
Anton Spirin, 2019-07-09
@WizardW

I usually do this:

store.dispatch(clientInit());

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

clientInit performs all the actions necessary to initialize the application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question