Server side rendering of reactjs for express? - reactjs

What is the best view engine/ method to pre-render a react component on an express backend? I'm at a loss at finding the most appropriate way to do it, all I can find in regards to resources are a bunch of article-tutorials, but they all do it in a different way, so I'm wondering if there is a recommended convention.
Essentially I just want to show 1 component with a bunch of pre-set props. No navigation or anything, just for 1 endpoint. I.e. You go to /component and it returns <Component prop1={1}/>

Just use the renderToString or renderToStaticMarkup methods if you just want the single component rendered and then send it in your response.
See: ReactDOMServer

Related

Fetching a react component from a server

Let's say I have an application with a component named "Protected"
**<Protected />**
This component should be "protected", and only admin users should have access to them (because it contains secret in formations)
But there are 2 problems.
I can't store it on the react application using a protected route on react router. (because my application can be easily reversed)
I can't fetch only the data from the server, I need to fetch the whole component.
I searched about React server side rendering but I didn't fully understand if it's possible.
Can I do this with React server side rendering? Are there more efficient ways?
Thanks !

Why do we need 2 entry points in server-side rendering?

I am exploring server-side rendering of React using Dot Net. I found out that we need 2 separate JS files for server-side rendering. One for client side and one for server side. I don't understand the reason.
1-we want to make sure that we never ever import any server side code into any file that will be eventually sent out to the browser. the reason for that, we might mistakenly leak an API key or some very sensitive data.
2-In client-side and server-side routing is handled differently. We will use the StaticRouter on the server. It is specifically made for use on the server side rendering. When we do the initial render of the app, it is going to be using the static router. When our application gets shipped down to the browser and it gets rendered a second time or hydrated on the browser, we will swap out to using the browserRouter instead. Browser router has the ability to look at the URL but StaticRouter needs to be told manually.
import { StaticRouter } from "react-router-dom";
<StaticRouter location={req.path} context={{}}>
3-configuration of redux is different. In server side, before we ship down our pages to browser, we need to make sure all the data fetch is complete, our components gets its data to be rendered.
4-By keeping client-side and server-side code separate, in the future we can switch our front-end framework or server-side easily.

Is data-reactroot relevant to the hydrate function in React?

I was trying to understand what's the difference between ReactDOMServer.renderToString() and ReactDOMServer.renderToStaticMarkup() on React 16.8.6.
This is what I understood:
renderToStaticMarkup() is used on the server side when you just want to render the markup and don't want to hydrate it on the client side. (https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup)
renderToString() is used on the server side when you want to use the ReactDOM.hydrate() function to hydrate the application on the client side. (https://reactjs.org/docs/react-dom-server.html#rendertostring)
So, if I'm right, the only difference between this two methods is that renderToString() adds the data-reactroot on the main element of the application. React reinforces this on the renderToStaticMarkup() documentation:
Similar to renderToString, except this doesn’t create extra DOM
attributes that React uses internally, such as data-reactroot. This is
useful if you want to use React as a simple static page generator, as
stripping away the extra attributes can save some bytes.
If you plan to use React on the client to make the markup interactive,
do not use this method. Instead, use renderToString on the server and
ReactDOM.hydrate() on the client.
But, I was reading this issue on React's repository where Dan Abramov said:
Instead, use hydrate() to explicitly tell React to hydrate existing
HTML. Then it won't depend on whether data-reactroot exists or not.
And:
...The suggested migration path is to use ReactDOM.hydrate() which
completely sidesteps the problem because it doesn't rely on the
data-reactroot attribute.
So, my question is: Is data-reactroot relevant to the hydrate function in React or is the documentation wrong?
Understanding this will be really helpful to fix a bug I'm having while hydration.

How to get html code from reactjs component

I need to send an email of my rendered reactjs component, for that, I need to convert my react component in HTML and send the email. I know how to send HTML through the mail, but getting stuck in how to get HTML from the reactjs component.
You can use renderToString of react-dom/server and use it like
import ReactDOMServer from 'react-dom/server'
const htmlString = ReactDOMServer.renderToString(<App />)
ReactDOMServer is used for SSR (Server Side Rendering) of react components.
renderToString converts your React component to string. So, You will get string html from JSX of your component.
There are two ways to do that. One is the renderToString() method, as the other answer mentions.
The other one is renderToStaticMarkup(): https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup
From docs:
Similar to renderToString, except this doesn’t create extra DOM attributes that React uses internally, such as data-reactroot. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save some bytes.
If you plan to use React on the client to make the markup interactive, do not use this method. Instead, use renderToString on the server and ReactDOM.hydrate() on the client.
For sending email you don't need extra DOM attributes or hydration since the email doesn't include any JS code, renderToStaticMarkup() will do the job.
To get HTML from a react component, you can use theReactDOMServer.renderToString method (doc) used mainly for SSR purpose. However I guess you can achieve what you want with it.

How to add universal support existing react project

I've created a react redux project, now how do I add some SEO functionalities to project? I do not want to waste much time while refactoring codes.
You need to setup the redux store on the server and pass its initial state down to the client, so that the server-render and initial client render don't differ
Not all life cycle functions are called on the serverside, mainly componentDidMount is not called. This indeed helps if you want to do some AJAX data fetching (which you only want to do on the client).
If you are using react-router you need to setup the serverside route matching
Here are some more details: React can be used on server side rendering. What does that mean?

Resources