Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  34] [ 1]  / answers: 1 / hits: 101067  / 9 Years ago, tue, august 11, 2015, 12:00:00

I am building an app with NodeJS and would like to use React for some of the interactive components across the application. I do not want to make it single page app.


How do I break up or bundle my React components across a multi-page app?


Currently all my components are in one file even though I may never load them in some sections of the app.


So far I am trying using conditional statements to render components by searching for the ID of the container where React will render. I am not 100% sure of what the best practices are with React. It looks something like this.


if(document.getElementById('a-compenent-in-page-1')) {
React.render(
<AnimalBox url="/api/birds" />,
document.getElementById('a-compenent-in-page-1')
);
}

if(document.getElementById('a-compenent-in-page-2')) {
React.render(
<AnimalBox url="/api/cats" />,
document.getElementById('a-compenent-in-page-2')
);
}

if(document.getElementById('a-compenent-in-page-3')) {
React.render(
<AnimalSearchBox url="/api/search/:term" />,
document.getElementById('a-compenent-in-page-3')
);
}

I am still reading the documentation and haven't found what I need yet for a multi page app.


More From » node.js

 Answers
6

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 = {
CommentBox,
OtherApp
};

function renderAppInElement(el) {
var App = APPS[el.id];
if (!App) return;

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

ReactDOM.render(<App {...props} />, el);
}

document
.querySelectorAll('.__react-root')
.forEach(renderAppInElement)





<div>Some Article</div>
<div id=CommentBox data-post_id=10 class=__react-root></div>

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


Edit



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[el.id]) {
apps[el.id]().then((App) => {
ReactDOM.render(<App {...el.dataset} />, el);
});
}
}

[#65454] Saturday, August 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jacklynr

Total Points: 542
Total Questions: 120
Total Answers: 95

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
jacklynr questions
Fri, Jan 15, 21, 00:00, 3 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
Thu, Jun 4, 20, 00:00, 4 Years ago
;