How is React JS rendered? - reactjs

This might be a silly question, but after reading many documents, I am still not sure. I know there are 3 main types of rendering:
Sever side rendering (render at request time)
Client-side rendering (render on client side)
Static site generation (render at build time)
I have some experience with Next JS, and I know you can choose which rendering type you want for a page in Next JS.
However, for React JS, from what I read here and there, it seems to be client-side render. But when I host a React webpage (say on S3), I would build it first and host the generated static contents on S3. Doesn't this mean React JS fall into the static site generation category? What is the "build action" doing anyway, and how is it different from rendering?

When you write your code you more than likely seperate your concerns and components into different files. With react you likely have a .js file for each component, you may also have a .css file etc etc. This is great for organising workflow, but not idea for a production app.
When you "build" your project, react takes all these files and merges them together and optimises for fast page loading. You are left with a build directory which contains the files ready for deployment.
Rendering happens as the page is loaded. It is the process of react determining what the UI will look like by checking what components need to be loaded and what state they are in.

Related

Exposing a React component to be imported to a different application

So i read somewhere about exposing a React component on the server which makes it importable to a different application, However im unable to find where i read this and cannot locate anything that would help me with this query.
So lets say i have several microservice applications they all essentially share the same Navbar component, my Goal would be to have the component in a central place that can be edited and updated and that would update all other components in other react applications, it would help with maintaining the code.
but in all actuality im not even sure this is possible. considering react components are built into the build folder any structural changes wouldnt be noticeable without rebuilding. But i know i read somewhere about this.

React Styleguidist - Web Component doesn't load on index.html

I have a style guide for a react web component library
When loading the base URL, the sections load as expected and everything works.
When I load the base URL/index.html (e.g. by switching back to "Components" after loading "Layers" in the top right tabs), the same page should be loaded, but the web component fails to load with no errors. When checking out the examples, errors are thrown, presumably because they can't access the web component.
My guess is some adjustments should be made in the style guide config. Does anyone have any hints? I am fairly new to web components, so I am pretty much in the dark atm
Thanx

Server side rendering react - Virtual DOM?

I recently started looking into SSR(Server side rendering) and I am impressed with its advantages. To name a few Load times, SEO, No javascript configuration.
However I am trying to understand if react server side rendering is worth it.
React is known for its Virtual DOM manipulations but using react with server side rendering will not give benefits of reactJs.
Can some one shed your ideas on using reactJS for server side rendering?
Using server-side rendering in React does not imply that React will not be used on client-side.
One of completely valid approaches is to start with client-side rendering only. In this case you have to setup a single HTML element in your HTML file that will become a hook for React once it loads.
Just to give you an example, let's say we have an <div id="root"></div> element in index.html file that will be served if we HTTP GET / path on our server. Initial document (in our case index.html) should also reference javascript file that includes React and our code. It can be done by adding something like <script type="text/javascript" src="/index.js"></script> just before </body> tag.
At some point while index.js is executed, ReactDOM.render() method is called (note: we are in the browser right now) - this is a moment in time when React looks for a div element with root id attached in a document. After it's found, it becomes react-root - component tree is mounted under this element and managed by React (ie. virtual DOM, event handlers, state updates).
Please note that this approach requires that at least one javascript file is fetched, parsed and executed before browser can render anything meaningful (other than an empty div) to a user. For some scenarios, this is not acceptable and this is where SSR (server-side rendering) can help.
Server-side rendering requires that you have JavaScript runtime environment available on your server. One of the popular choices is Node.js (others include for example Nashron for JVM).
In approach, you execute React on the server and use ReactDOMServer.renderToString() (or ReactDOMServer.renderToNodeStream()) method to generate HTML response that is sent to the client - instead of an almost empty response with just one placeholder div as previously, now you can send all the markup that will be generated from your component tree (important note here is that in React 16.4(+) only UNSAFE_componentWillMount lifecycle method is called on server-side). After the initial response with a document is sent to the client, browser can render the initial markup before index.js even finishes downloading. Once it does, ReactDOM.hydrate() method kicks in. Hydration is a process of using existing server-side rendered markup and "watering" it with javascript goodies like event handlers. After it's done, this component tree is now completely managed by React with all the benefits.
Please note that in SSR, exactly the same component tree is rendered on a server-side and that's then hydrated on a client-side.
Of course, React can also be used instead of templating engines as a very powerful static HTML markup generator. All you need to do is to render the markup on the server with ReactDOMServer. renderToStaticMarkup() and send it to the client. It should be noted that this approach has a significant performance impact (https://malloc.fi/performance-cost-of-server-side-rendered-react-node-js) and uses a very limited number of React features.

Prevent mounting/initial rendering of a server rendered react component

I'm using a custom tool similar to react-snap to create a snapshot of my app at build time, as recommended in the create-react-app docs. This generates a static server-rendered version, which I can deploy behind nginx without running react on the server. This works fine.
I'm also using code-splitting to lazily load some components to reduce the initial JS payload. (I use react-loadable, but I'm willing to change that if needed.) It works fine when creating the snapshot, and the HTML is generated correctly, which the user receives correctly, and everything is displayed fully server-rendered without even downloading any JS yet. This is great.
However, during rehydration on the client, the import(...) call for the async loaded component hasn't yet been fired. Meanwhile, the DOM already has the stuff that has to be rendered, from the snapshot.
In this scenario, is there a way to prevent the initial render of a component during mounting, since I already have the content in the DOM?
Also, is it possible to have react's hydration logic run after the import is complete, so that I can prevent any flicker even after the import is complete?
If it matters, the lazy-loaded components are infrequently used routes, managed by react-router.

Web components with common dependencies

I have a number of web components consumed in a separate application. They all depend on React. React is not in the parent App, but all of the components will depend on React. Is it expensive to call and load React in each component, even over CDN? Or is there a better way to specify a common dependency shared by multiple web components?
Your browser will keep React in cache so even if you call it several times it will be downloaded once. You can see it in Chrome Dev Tools (HTTP code 304 Not Modified). So it shouldn't impact overall performances.
If you use the Web Component technology you could leverage HTML Imports to load dependancies.
You could also use a module loader like RequireJs.

Resources