combine / universal-react-redux

🧐 A sensible universal starter kit for React + Redux
110 stars 18 forks source link

[Question] Pre fetch data before react renders #53

Closed vianhanif closed 6 years ago

vianhanif commented 6 years ago

Similarly, this is a feature that Next.js has provided with getInitialProps function (if you might already know, it basically giving us the possibility to run redux actions to prefetch data from backend, so the component will already has data to render with). Has anyone tried it ? any help would be great because Next.js is the last thing I want to use for my work. Thanks~

calvinl commented 6 years ago

@vianhanif This is also a feature in this boilerplate. You can use a static fetchData method on your component to pre-fetch on the server side. See https://github.com/combine/universal-react-redux#pre-fetching-data-for-server-side-rendering-ssr

Closing for now. Feel free to reopen if I've misinterpreted your question.

vianhanif commented 6 years ago

ooh hha, you're right... stupid me. sorry for the hassle. Thanks 😄

vianhanif commented 6 years ago

@calvinl what other objects available from fetchData other than the store object?

calvinl commented 6 years ago

See here: https://github.com/combine/universal-react-redux/blob/master/server/renderer/handler.js#L116

fetchData is called with an object that includes: { store, route, match }, where match is the object returned by react-router's matchPath