Closed michaelmota closed 5 years ago
With the example I gave here, you could get the state using store.getState();
Yes I tried that but how I can make it work correctly, because when I use: store.getState(state.url) it says: "Cannot read property 'url' of undefined", and on the store I'm declaring the state and exporting it.
Again sorry for my ignorance, could you see if I have anything wrong here on the way I'm calling getState?:
static async getInitialProps({ store, state }) {
store.getState(state.url);
const res = await fetch(
'https://api.github.com/repos/ooade/NextSimpleStarter'
)
const json = await res.json()
return { stars: json.stargazers_count }
}
store.getState() returns an object i.e. the global states. So you could do store.getState().user, and so on... But if you don't plan on using it in getInitialProps, you can use the connect function from react-redux to mapStateToProps, and get the value through this.props.
Closing this issue, solved it with the mapStateToProps. Thanks @ooade !
Hello,
I used this boilerplate to migrate a project made with Create-React-App using Redux, and I have to say that it's been a complete headache understanding the SSR with React, but I'm almost done with it. I'm new using NextJS and I want to know how can I get the state from the Store, for example I want to use the URL prop on my ComponentWillMount of Blog.
Here is my store:
and here is the component where I want to get the state for the URL for example:
Any ideas of how to get this done would help me a lot.