A
A
Andrey Okhotnikov2019-05-20 14:09:52
React
Andrey Okhotnikov, 2019-05-20 14:09:52

How to pass data to redux on server side render?

nextjs. On the index.js route page, I call an action through getInitialProps that updates the redux store.

Index.getInitialProps = ({ reduxStore }) => {
  reduxStore.dispatch(toggleType('woman'));
  return { };
};

Store is being updated. The received data is available through props. But an empty state arrives in the browser, and the components subscribed to the store via connect do not see the received data.
const mapStateToProps = state => ({
  type: state.app.type,
});

export default connect(
  mapStateToProps
)(withRouter(HeaderContainer));

So, in this case, how to transfer the data received in getInitialProps to the browser?
I see only one way - in the page component in componentDidMount, pass data from props to the store, but this is hardly the best option, because re-render will be called

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question