// Register server-side rendering middleware
app.get('*', (req, res) => {
if (__DEV__) {
webpackIsomorphicTools.refresh();
}
const store = configureStore();
// If __DISABLE_SSR__ = true, disable server side rendering
if (__DISABLE_SSR__) {
res.send(renderHtmlPage(store));
return;
}
const memoryHistory = createMemoryHistory(req.url);
const history = syncHistoryWithStore(memoryHistory, store, {
selectLocationState: state => state.get('routing').toJS(),
});
// eslint-disable-next-line max-len
match({history: memoryHistory, routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (!renderProps) {
res.sendStatus(404);
} else {
// Dispatch the initial action of each container first
const promises = renderProps.components
.filter(component => component.fetchData)
.map(component => component.fetchData(store.dispatch, renderProps.params));
// Then render the routes
Promise.all(promises)
.then(() => {
// Using the enhanced history of react-redux-router to instead of the 'memoryHistory'
const props = Object.assign({}, renderProps, { history });
const content = renderToString(
<Provider store={store}>
<RouterContext {...props} />
</Provider>
);
res.status(200).send(renderHtmlPage(store, content));
});
}
});
});
твой компонент который загружается при переходе на роут должен иметь статический метод fetchData, он может выглядеть примерно так
class UsersPage extends React.Component {
static async fetchData(url) {
try {
const res = await axios.get(url);
const users = res.data
} catch(err) {
console.error(err.message);
}
}
componentDidMount() {
this.fetchData(url);
}
render() {
return (
// whatever
);
}
}