I am fairly new to react and trying to make a redirect using react-router-dom I did everything as the documentation but my code doesn't seem to work. I get an error regarding no element found which I don't understand what is causing it. Also, I am just trying to reach the page using the address bar and not any button or link for now.
Error
Matched leaf route at location "/" does not have an element. This means it will render an <Outlet /> with a null value by default resulting in an "empty" page.
Same error for /register
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom';
import Register from "./components/Register";
ReactDOM.render(
<Router>
<React.StrictMode>
<Routes>
<Route exact path="/register" component={Register} />
<Route exact path="/" component={App} />
</Routes>
<App />
</React.StrictMode>
</Router>,
document.getElementById('root')
);
reportWebVitals();
App.js
import { Component } from 'react';
class App extends Component {
render(){
return (
<div>
HELLO WORLD
</div>
);
}
}
export default App;
Register.js
import { useState } from "react";
import { useNavigate } from "react-router-dom";
export default function Register(){
const history = useNavigate();
const initialFormData = Object.freeze({
email: '',
password: '',
});
const [formData, updateFormData] = useState(initialFormData);
const handleChange = (event) => {
updateFormData({
...formData,
// Trimming any whitespace
[event.target.name]: event.target.value.trim(),
});
}
const handleSubmit = (event) => {
event.preventDefault();
console.log(event.target.email.value);
console.log(event.target.password.value);
}
return (
<form onSubmit={handleSubmit}>
<div>
<label>
Email:
<input type="text" value={formData.email} name="email" onChange={handleChange} />
</label>
</div>
<div>
<label>
Password:
<input type="text" value={formData.password} name="password" onChange={handleChange}/>
</label>
</div>
<input type="submit" value="Submit" />
</form>
);
}