I created a basic react app with
npx create-react-app
I can create components in the index.js
and I'm able to render them to an element in the index.html
.
However that's the only page I can render them too, I can't for example use the same component on the login.html
page or any other page other than index.html
I'm testing this with npm start
.
Commonly React is used for single page applications, so you just render the app in a single page, then use routing (with
react-router
commonly) for simulate browser navigation.However you can render it on multiple pages... you only need to copy your activation JavaScript on other pages
Just copy this on your
login.html
(taken from default create-react-app output):Why would you need this? Remember that React, for his own nature, claims to be a SPA (Single Page Application). This should be enough to let you understand that the single page is (should be) index.html.
Anyway, you can do it by looking at the default index.html file created by create-react-app and also at the manifest.json file.
Starting by the manifest.json, you should have this JSON:
Or something like this. You can se a "start_url" property, which is the starting point that create-react-app webpack dev-server will, by default, serve to you when you request ANY path at the local address you are running it (localhost:3000 normally). By changing this property, you can make it serve the page you want, e.g. login.html.
Now, looking at index.html default code you should have something like this:
As you can see, at the end of the page there is a
Which is exactly where React renders what ReactDOM.render(jsx, ElementID) jsx is. In fact, if you look in index.js, ReactDOM is like:
You can see that ElementID ('root') is the id of the div element where you want to render the jsx (). If you create a new page, say login.html, copy-paste the index.html content and change the element ID, you can then choose to render your content there changing also the referral ElementID of ReactDOM.render(). Working on this you should get the result you want to, but as said before, in my opinion you shouldn't need this.