I'm currently working on a React application where I'm using React Router for navigation. However, I'm facing an issue where the components associated with routes are not rendering properly.
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from '../components/App';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
document.addEventListener('DOMContentLoaded', () => {
const rootElement = document.getElementById('root');
if (rootElement) {
ReactDOM.render(
<Router>
<Routes>
<Route path="/" element={<App />} />
</Routes>
</Router>,
rootElement
);
document.body.appendChild(document.createElement('div'));
}
});
App.js
import React from "react"
import {Route, Routes, } from "react-router-dom"
import Gyms from "./Gyms/Gyms"
import Gym from "./Gym/Gym"
const App = () => {
return (
<Routes>
<Route path="/" component={Gyms}/>
<Route path="/gyms/:slug" component={Gym}/>
</Routes>
)
}
export default App;
Gym.js
import React from 'react'
const Gym = () => {
return <div>This is the Gym#show view for our app.</div>
}
export default Gym
Gyms.js
import React from 'react'
const Gyms = () => {
return <div>This is the Gyms#index view for our app.</div>
}
export default Gyms
Despite setting up the routes as above, when I navigate to the routes, I'm not seeing the expected components rendering. Instead, I just see a blank page.
There's an issue with your index.js file it seems. I've used basic codesandbox template for react which contains specific index file
Along with that pass an element as @Puttsche suggested like below
And it worked for me. Here's sandbox working example with your code.
Sandbox Link