Open
Description
Hi there! Great lib. Having trouble getting state from api call while doing server side render. All i get is the initial state of the reducer. How to wait for store to get a new state? (using redux-logic, recompose)
// Component
const enhancer = compose(
connect((state, { match }) => {
return {
data: pageGeneratorDataSelector(state, match.params.id),
meta: pageGeneratorMetaSelector(state, match.params.id)
}
}),
withLifecycle({
onWillMount({ match, dispatch }) { // or DidMount, no matter
dispatch(actions.data(match.params.id))
}
}),
shouldUpdate(
({ data, meta }, { data: dataNext, meta: metaNext }) =>
!shallowEqual(data, dataNext) || !shallowEqual(meta, metaNext)
)
)
export const PageGeneratorProvider = enhancer(({ data, meta }) => {
console.log('PageGeneratorProvider -> data', data) // here i get empty array from initial data on first render SS
return <PageGenerator data={data} meta={meta} />
})
// Logic
export const getPageSchemaLogic = createLogic({
type: actions.data().type,
debounce: 300,
latest: true,
process({ action }, dispatch, done) {
pageGeneratorApi
.getPageSchema(action.payload)
.then(results => dispatch(actions.success(results))) // updating data
.catch(err => dispatch(actions.fail(err)))
.then(() => {
return done() // I guess here whenComplete resolves, but store still not updated.
})
}
})
// Server renderer
const root = (
<ServerRoot
location={req.url}
sheet={sheet.instance}
store={store}
context={context}
/>
)
store.logicMiddleware.whenComplete(() => {
const jsx = extractor.collectChunks(root)
const html = renderToString(jsx)
const styleTags = sheet.getStyleTags()
const scriptTags = extractor.getScriptTags()
const linkTags = extractor.getLinkTags()
const storeState = store.getState()
const preloadedState = encodeURIComponent(JSON.stringify(storeState))
res.status(200).send(
renderFullPage({
html,
styleTags,
scriptTags,
linkTags,
preloadedState,
meta
})
)
})
Like whenComplete ignore logic completion