How to migrate server side rendering logic to react router 4 ? (Redux)
See original GitHub issueI am trying to migrate my server side rendering logic to react router 4.But it seems like there many properties are missing like renderProps.
server.js with Old react-router
app.get('/*', (req, res,next) => {
// Server Side Rendering Starts
match({routes:routes(),location:req.url},(err,redirectLocation,renderProps) => {
if (err) return next(err);
if (redirectLocation) {
return res.redirect(302, redirectLocation.pathname + redirectLocation.search)
}
if (!renderProps) {
return next(new Error('Missing render props'))
}
const components = renderProps.components
const Comp = components[components.length - 1].WrappedComponent;
const fetchData = (Comp && Comp.fetchData) || (() => Promise.resolve())
const initialState = {}
const store = createStore(reducer, initialState, applyMiddleware(thunk));
const { location, params, history } = renderProps
fetchData({ store, location, params, history }).then(() => {
const body = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
)
const state = store.getState();
// console.log(state)
let head = Helmet.rewind();
res.send(`<!DOCTYPE html>
<html>
<head>
${head.title}
${head.meta}
${head.link}
</head>
<body>
<div id="app" >${body}</div>
<script>window.__STATE__=${JSON.stringify(state)}</script>
<script src="/bundle.js"></script>
</body>
</html>`)
})
.catch((err) => next(err))
})
});
NewServer.js
app.get('*',(req,res) => {
// res.sendFile(path.join(__dirname,'./index.html'));
const routerContext = {};
const components = (
<StaticRouter location={req.url} context={routerContext}>
<Provider store={store}>
<CoolApp/>
</Provider>
</StaticRouter>
)
//I don't Knnow
});
I don’t know how to proceed without renderPorps
This is my Sample Component
import React,{Component} from 'react';
import {Helmet} from 'react-helmet';
import {connect} from 'react-redux';
import * as allPosts from '../Redux/Actions/AllPosts_action'
class Home extends Component{
renderAllPosts(){
const {posts} = this.props;
return(
posts.postArr.map(({id,title,body}) => {
return <li key={id}>{title}</li>
})
)
}
render(){
console.log(this.props);
return(
<div>
<Helmet>
<meta charSet="utf-8" />
<title>My Title</title>
<link rel="stylesheet" type="text/css" href="/static/css/Home.css" />
</Helmet>
{this.renderAllPosts()}
</div>
)
}
}
Home.fetchData = ({store,location, params, history}) => store.dispatch(allPosts.getAllPosts());
const mapStateToProps = (state) => {
return{
posts:state.allPosts.toJS()
}
};
const mapDispatchToProps = (dispatch) => {
return {
getAllPosts:() => dispatch(allPosts.getAllPosts())
}
};
export default connect(mapStateToProps,mapDispatchToProps)(Home)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:13 (4 by maintainers)
Top Results From Across the Web
How to migrate server side rendering logic to react router 4 ...
Then through the components array to fetch data before server rendering. //fetchData.js //use the component.need node to fetch data. export ...
Read more >Server-Side Rendering with Redux and React-Router
Learn how to use Redux and React-Router to do server-side rendering.
Read more >Server-side rendering with React Router v4 and Redux
In this post I'll explain one way you can implement server-side rendering (SSR) for an app that's using React Router v4 and Redux...
Read more >ReactJS : Server side rendering with router v4 & redux
jsx import React from 'react'; import { Route, Redirect } from 'react-router-dom'; const ListToUsers = () => { return ( <Route render={({ ...
Read more >Server Rendering - Redux - JS.ORG
The final step on the server side is to inject our initial component HTML and initial state into a template to be rendered...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This is a bug tracker, not a support system. For usage questions, please use Stack Overflow or Reactiflux. Thanks!
@iNikNik it’s so stupid! stupid! stupid! IT DOESN’T WORK! Understand? Where is store? Where are routes? I hate all this stuff!