I am using Next.js and would like to prefetch an API item using redux. Here's some code that almost works:
class Thing extends Component {
static getInitialProps ({ store }) {
store.dispatch(fetchProduct())
}
}
const mapStateToProps = (state, ownProps) => {
return {
product: getProduct()
}
}
I am running into the issue that the component renders before getProduct
has info. How can I shape things so that I don't render until dispatch has fetched the item? I am using redux-api-middleware in case that matters. I can check for the existence/validity of 'product' in render but that sort of defeats the purpose of getInitialProps
. Is there a way to achieve the equivalent of an async/await fetch with redux?
Update Ok this might not work. redux-api-middlewar seems to return an empty object as an action on SSR - so its not a timing issue.