You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to do SSR with Express and React and I want to Dynamically create the routes for this application I have recently been having a lot of trouble with the initial render I have based this section of my code on the examples I provided but they both use 'match' from react router but it is no longer supported.
I have been searching for a good replacement for it for v6 but can't seem to find a good one.
I've seen the new documentation for RRDv6 using StaticRouter but I want to do it in a more dynamic way is that possible currently or do I have to use StaticRouter?
`import fs from 'fs';
import path from 'path';
import React from 'react';
import { ReactDOMServer, renderToString } from 'react-dom/server';
import router from '../routes/C_Router'
import { RoutingContext, match } from 'react-router';
import { redirect } from 'react-router-dom';
import { Provider } from 'react-redux';
import store from '../redux/store'
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am trying to do SSR with Express and React and I want to Dynamically create the routes for this application I have recently been having a lot of trouble with the initial render I have based this section of my code on the examples I provided but they both use 'match' from react router but it is no longer supported.
I have been searching for a good replacement for it for v6 but can't seem to find a good one.
I've seen the new documentation for RRDv6 using StaticRouter but I want to do it in a more dynamic way is that possible currently or do I have to use StaticRouter?
`import fs from 'fs';
import path from 'path';
import React from 'react';
import { ReactDOMServer, renderToString } from 'react-dom/server';
import router from '../routes/C_Router'
import { RoutingContext, match } from 'react-router';
import { redirect } from 'react-router-dom';
import { Provider } from 'react-redux';
import store from '../redux/store'
//-Inital Render-//
',export const initial_render = (req, res, next) => {
fs.readFile(path.resolve('./public/index.html'),(err, data) => {
match({router, location: req.url}, (err, redirect, renderProps) => {
if(err) {
console.log(err.message);
return res.send(500).end('Internal Server Error');
}
if(!renderProps) return res.status(404).end('Page Not Found');
const store = store()
return res.send(data.replace(
'
<div id="root">${ReactDOMServer.renderToString( <Provider store={store}> <RoutingContext {...renderProps} /> </Provider> )}</div>
))})
})
}`
Example 1: https://github.com/caljrimmer/isomorphic-redux-app/blob/master/src/server/server.js
Example 2: https://github.com/remix-run/react-router/blob/v1.0.0-rc3/docs/guides/advanced/ServerRendering.md
Beta Was this translation helpful? Give feedback.
All reactions