Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a React app in a React app (nested React apps)

So the question is if it is possible to split a React app into two different separate apps hosted on two different hosts, where the app A is a kind of a frame which controls the app B and C in the future. I have a problem, where I would like to make a common fundament for both apps (the A app) and then load two other as a content of it. It would be as if I had lazy loading with a bundle fetched from a different place. I was thinking about three main possibilities:

  • Iframe
  • Single SPA project from github
  • using ReactDOM.render method

I am not sure if it is possible at all, beacuse there still may be a problem with React Router - does the inside app have access to manipulate the browser routing?

like image 248
mikes Avatar asked May 08 '18 12:05

mikes


2 Answers

This is not possible. Each react app can only have a single package.json in the hierarchy. if you nest it, the app will fail and say you have several installs of react. what you should do is think more react minded and objecty. You can have a folder for common components to share inside src/. You can also have src/A which is one "app". src/B which is another.

What you described in your question is exactly what you should do, just dont think of it as a react app separation, rather a seperation of component and app inside the src folder. App A can be comprised of components from /components as well as App B.

like image 152
Jesse Avatar answered Oct 18 '22 15:10

Jesse


It is quite possible to split your react Application into multiple smaller react applications.

Suppose you have a react application such as an e-commerce platform . You can choose to write the cart Page using a separate react-App and the products page using another separate react app and integrate them together using Module Federation Plugin webpack/lib/container/ModuleFederationPlugin.

A good reason to do something like that would be to develop different parts of your application in isolation ..and they can be taken care by different teams altogether.

There is a udemy course that teaches you exactly that. Very much recommended. You can make react dependency as singleton to avoid several installs of react.

like image 2
Avan Avatar answered Oct 22 '22 20:10

Avan