I created a React app and it ran perfectly fine with simple HTML/CSS a header and a button. But when I added <Routes>, <Route> and navigation.navigate, I get a blank screen with no errors in the console that I used to run the app. However, there is an error in the browser console, stating "Cannot read property of undefined 'getElementById".
I have tried giving the main parent div id='root' as that is the variable that was passed to getElementById. I am not sure what other solutions to try as other questions have had different situations/contexts.
Sample code is here https://codesandbox.io/s/frosty-fast-zqcn83?file=/src/index.js . You can see the error message, and if you exit it, you can see the blank screen (with the colour that I chose).
Code:
import './App.css';
import {Routes, Route, Router, useNavigate, NavLink, Link, BrowserRouter} from 'react-router-dom';
import Newpage from './screens/Newpage.js'
function App() {
let navigate = useNavigate();
function navigateToCloset(){
navigate(Newpage);
}
const About = () => (
<div className='about'>
<h1>About Me</h1>
<p>Ipsum dolor dolorem consectetur est velit fugiat. Dolorem provident corporis fuga saepe distinctio ipsam? Et quos harum excepturi dolorum molestias?</p>
<p>Ipsum dolor dolorem consectetur est velit fugiat. Dolorem provident corporis fuga saepe distinctio ipsam? Et quos harum excepturi dolorum molestias?</p>
</div>
);
const Contact = () => (
<div className='contact'>
<h1>Contact Me</h1>
<p>You can reach me via email: <strong>hello@example.com</strong></p>
</div>
);
return (
<BrowserRouter>
<div id='root'>
<div className='header'>
My Closet
</div>
<div className='homepagebuttondiv'>
<button className='homepagebutton'
onClick={navigateToCloset}
>
Enter Closet
</button>
<button className='homepagebutton'>
New Clothing
</button>
</div>
<Routes>
<Route path="about" element={<About />}/>
<Route path="contact" element={<Contact />} />
</Routes>
<nav>
{/* <Link to="/">home?</Link> */}
<Link to="/about">About</Link>
</nav>
</div>
</BrowserRouter>
);
}
export default App;