Accepted answer

currently, i am doing something similar.

the application is not a full react app, i am using react for dynamic stuff, like commentbox, which is autark. and can be included at any point with special params..

however, all my sub apps are loaded and included into a single file all.js, so it can be cached by the browser across pages.

when i need to include an app into the ssr templates, i just have to include a div with the class "__react-root" and a special id, ( the name of the react app to be rendered )

the logic is really simple:

import commentbox from './apps/commentbox';
import otherapp from './apps/otherapp';

const apps = {

function renderappinelement(el) {
  var app = apps[];
  if (!app) return;

  // get props from elements data attribute, like the post_id
  const props = object.assign({}, el.dataset);

  reactdom.render(<app {...props} />, el);


<div>some article</div>
<div id="commentbox" data-post_id="10" class="__react-root"></div>

<script src="/all.js"></script>


since webpack perfectly supports code-splitting & lazyloading, i thought it make sense to include an example where you don't need to load all your apps in one bundle, but split them up and load on demand.

import react from 'react';
import reactdom from 'react-dom';

const apps = {
  'one': () => import('./one'),
  'two': () => import('./two'),

const renderappinelement = (el) => {
  if (apps[])  {
    apps[]().then((app) => {
      reactdom.render(<app {...el.dataset} />, el);


i suggest you take a look at inertiajs:

with inertia you build apps just like you've always done with your server-side web framework of choice. you use your framework's existing functionality for routing, controllers, middleware, authentication, authorization, data fetching, and more.

the only thing that's different is your view layer. instead of using server-side rendering (eg. blade or erb templates), the views are javascript page components. this allows you to build your entire front-end using react, vue or svelte.


i revive this old question since i was in the same situation, without finding an answer that could satisfy my needs. so, based on @webdeb 's answer, i wrote a mini-framework that use cra (without eject) to inject as many components you want in any html page while preserving all the cra's benefits.


you can check my public repo here that contains all the needed files and a link to a medium article where i thoroughly explain all this stuff.

the general idea

the trick is to install cra as you normally would, and update the index.js file as follows :

import react, { suspense } from 'react';
import reactdom from 'react-dom';
import './index.css';
import reportwebvitals from './reportwebvitals';

//list here all the components that could be inserted in a web page
const apps = {
  'app': react.lazy(() => import('./app')),
  'testcomponent1': react.lazy(() => import('./testcomponent1')),
  'testcomponent2': react.lazy(() => import('./testcomponent2')),

//event manager to communicate between the components
const bridgeevent = new eventtarget();
//common fallback for all the components
function fallback() {
  return <div>loading...</div>;
const renderappinelement = (el) => {
  if(apps[el.dataset.reactcomponent] && !el.dataset.rendered){
    //get the component's name stored in the data-react-component attribute
    const app = apps[el.dataset.reactcomponent];
    //render the component, inject all the html attributes and the event bridge
      <suspense fallback={<fallback />}>
        <app  {...el.dataset} bridgeevent={bridgeevent}/>
    , el);
    el.dataset.rendered = true;
  else if(el.dataset.rendered){
    console.log('el', el, 'is already rendered')

//only for the dev phase
const rootel = document.getelementbyid('root');
//generate components without attributes
  const components = process.env.react_app_render_cmp.split(',');
  components.foreach(item => {
    const componentel = document.createelement('div');
    componentel.setattribute("data-react-component", item);
    componentel.classname = "__react-cmp";
//generate components with attributes
  let componentswithattrs;
    componentswithattrs = json.parse(process.env.react_app_render_cmp_with_attrs);
    console.log('fail to parse react_app_render_cmp_with_attrs', e);
    componentswithattrs.foreach(cmp => {
      const componentel = document.createelement('div');
      componentel.setattribute("data-react-component", cmp.class);
      componentel.classname = "__react-cmp";
      object.keys( => {

//the default name of the global object is reactcomponents, but it could be customized via the react_app_namespace environment variable
const appnamespace = process.env.react_app_namespace || "reactcomponents";
window[appnamespace] = {
  ready: false,
    //parse the container or the whole document and inject all the components in the containers that have a "__react-cmp" class
    (container || document)
window[appnamespace].ready = true;

//if dynamic parsing must be done via the window.reactcomponents.parsecomponents() method
//check the availability of window.reactcomponents object via window.reactcomponents.ready property
//or define a window.reactcomponentsasyncinit() method to be notified of the availability
if(typeof window[`${appnamespace}asyncinit`] === 'function'){

// if you want to start measuring performance in your app, pass a function
// to log results (for example: reportwebvitals(console.log))

then you can add react_app_render_cmp and/or react_app_render_cmp_with_attrs environment variables to test your components while using the cra's development server. your .env.development.local file could look like:

#this will render the testcomponent1 and testcomponent2 without any attributes

#this will render testcomponent1 with the data-test-attribute attribute set to "test attribute value"
react_app_render_cmp_with_attrs="[{"class":"testcomponent1","data":{"data-test-attribute":"test attribute value"}}]"

after building your files, you should have your index.html file with all the .js and .css files you need to include in each page of your multi-page app that should load your react components. don't forget to add inline_runtime_chunk=false in your .env file to avoid any inline javascript!

then, add the components' containers in the html pages where you want them to show. for instance:

<div class="__react-cmp" data-react-component="testcomponent1"></div>

the parsecomponents() declared in the cra's index.js file should be executed, grabbing your div with the .__react-cmp class, then use it as a container for your testcomponent1 react component.

in the dedicated repo and article i explain how you could change your build path with the cra's build_path environment variable (so you can host your built files in your server or in a cdn) and i provide a loader that will parse the built index.html file and dynamically insert all the needed .js and .css files in your page (so you just have to include the loader, instead of all the files). here is how the loader looks like, assuming its file name is cmp-loader.js and hosted next to your built index.html file:

(async () => {
  const head = document.getelementsbytagname('head')[0];
  const scriptsrcregexp = new regexp('<script.*?src="(.*?)"', 'gmi');

  //get the exact script's src as defined in the src attribute
  const scriptsrc = scriptsrcregexp.exec(document.currentscript.outerhtml);
  //all the resources should be relative to the path of this script
  const resourcespath = (scriptsrc && scriptsrc.length > 1) ? scriptsrc[1].replace('cmp-loader.js', '') : '';

  //get the index content
  const indexhtml = await (await fetch(resourcespath+'index.html', {cache:'reload'})).text();

  //assume that all the .js and .css files to load are in the "static" folder
  const reactcssregexp = new regexp(`<link href="${resourcespath}static\/css\/(.*?)\.css" rel="stylesheet">`, 'gm');
  const reactjsregexp = new regexp(`<script (.*?) src="${resourcespath}static\/js\/(.*?)\.js"><\/script>`, 'gm');

  //grab all the css tags
  const reactcss = [].concat(indexhtml.match(reactcssregexp)).join('');
  //grab all the js tags
  const reactjs = [].concat(indexhtml.match(reactjsregexp)).join('');

  //parse and execute the scripts
  const scriptsdoc = new domparser().parsefromstring(reactjs, 'text/html');
  array.from(scriptsdoc.getelementsbytagname('script')).foreach(item => {
    const script = document.createelement('script');
    [...item.attributes].foreach(attr => {
      script.setattribute(, attr.value)
  //inject the css
  head.insertadjacenthtml('beforeend', reactcss);
})().catch(e => {
  console.log('fail to load react-cmp', e)


i know it's been a while since this question was asked but hopefully this helps someone.

as @cocomico mentioned you could provide several entry points for the application in the webpack.config.js file. if you are looking for a simple webpack setup (based on the idea of multiple entry points) that allows you to add react components to static pages you may consider using this:


are you using a cms? they tend to like changing urls which could break your application.

another way is using something like react habitat.

with it, you can register components and they automatically get exposed to the dom.


register component(s):

container.register('animalbox', animalbox);
container.register('animalsearchbox', animalsearchbox);

then they are availiable in your dom like this:

<div data-component="animalbox"></div>

<div data-component="animalsearchbox"></div>

the above will be automatically replaced with your react components.

you can then automatically pass properties (or props) to your components too:

<div data-component="animalbox" data-prop-size="small"></div>

this will expose size as a prop to your component. there are additional options for passing other types such as json, array's, ints, floats etc.


i'm building an application from the ground up and am learning as i go, but i think what you are looking for is react-router. react-router maps your components to specific urls. for example:

        <route path="/" component={app}>
            <route path="api/animals" component={animals}>
               <route path="birds" component={birds}/>
               <route path="cats" component={cats}/>
        <route path="api/search:term" component={animalsearchbox}>
), document.body)

in the search case, 'term' is accessible as a property in the animalsearchbox:

componentdidmount() {
    // from the path `/api/search/:term`
    const term = this.props.params.term

try it out. this tutorial is the one that put me over the top in terms of my understanding of this and other related topics.

original answer follows:

i found my way here looking for the same answer. see if this post inspires you. if your application is anything like mine, it will have areas that change very little and varies only in the main body. you could create a widget whose responsibility it is to render a different widget based upon the state of the application. using a flux architecture, you could dispatch a navigation action that changes the state your body widget switches upon, effectively updating the body of the page only.

that's the approach i'm attempting now.


you can provide several entry points for the application in the webpack.config.js file:

var config = {
  entry: {
    home: path.resolve(__dirname, './src/main'),
    page1: path.resolve(__dirname, './src/page1'),
    page2: path.resolve(__dirname, './src/page2'),
    vendors: ['react']
 output: {
    path: path.join(__dirname, 'js'),
    filename: '[name].bundle.js',
    chunkfilename: '[id].chunk.js'

then you can have in your src folder three different html files with their respective js files (example for page1):

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>page 1</title>
  <div id="app"></div>
  <script src="./vendors.js"></script>
  <script src="./page1.bundle.js"></script>

javascript file:

import react from 'react'
import reactdom from 'react-dom'
import app from './components/app'
import componenta from './components/reactcomponenta'
                  <app title='page1' />
                 </div>, document.getelementbyid('app'))

different react components can be then loaded for each single page.

Related Query

More Query from same tag