when pages are rendered at server-site at first time or refresh the rendered page, getInitialProps do not work:
Home.getInitialProps = async ({store}) => {
axios.post('/user').then(res => {
var user = res.data;
store.dispatch(setLoggingState(user));
}, res => {
console.log('4444');
})
return {};
}
In above code, server always print ‘4444’,and express did not receive the 'POST' request. tanks for your help
getInitialProps, because a relative url cannot work on the server-side.The getInitialProps method should use async and await, like this:
Home.getInitialProps = async ({store}) => {
await axios.post('/user').then(res => {
var user = res.data;
store.dispatch(setLoggingState(user));
}, res => {
console.log('4444');
});
return {};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With