The project I am currently working on is the first React project i’ve worked on. I have a lot of experince with backend and some with front end. The backend I have already written strongly relies on diffrent html pages / end point urls. (There are other Stackoverflow posts about a simular topic but I would rather not have to eject my program.
My current render:
import React, { StrictMode } from "react";
import ReactDOM from 'react-dom';
import "./styles.css";
import Login from "./Pages/Login"
import Reset from './Pages/Reset';
ReactDOM.render(<StrictMode><Login /></StrictMode>, document.getElementById('login'));
ReactDOM.render(<StrictMode><Reset /></StrictMode>, document.getElementById('reset'));
Then I have two html files one that has a div login and one that has div reset but only the index.html or login one works when i move the built files to the backend.
(I use react because it makes it a lot easier to make clean webpages)
I haven’t really tried very much, I have looked into other ways but all the tutorials / articals I have found have been very outdated.
4
Answers
you can use:
Routes, Route to make Route for change between different pages:
React is used to build, specifically, Single Page Applications (SPAs). So your attempt at building two
index.html
, i.e. two different SPAs seems strange.Since you’ve decided to build two different index.html, you can do this by building each component one at a time by commenting the other
ReactDOM.render
line.I think you should only have one HTML to setup React/JS which should have only
<div id="root"></div>
(id name should be constant with the js id). The two HTML that you have, u should rewrite them in JSX and either have them in the js file that you use here or have each of them have a js file and link them. Here is an example of how it should be.import React from ‘https://esm.sh/[email protected]’
import ReactDOM from ‘https://esm.sh/[email protected]’
In React you only need single
index.html
file.public/index.html
In order to make multiple urls work in React, use a library called
react-router-dom
.index.js
Remaining html files you want as pages, you can convert to React components files like below.
Login.js
Reset.js
Full source code including build and deploy using docker: https://github.com/hisanibrahim/react-router-app