I am trying to redirect to another page after the onSubmit and setRoom variables are received from the user's input. However, once I press submit the page reloads and a question mark appears next to the URL that was previously there without changing the page. I've included examples below.
import { useContext, useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import { UserAuth } from "../context/AuthContext";
import spacesLogo from "../img/SpacesLogo.png";
import { RoomContext } from "../context/RoomContext";
const JoinRoom = () => {
const [room, setRoom] = useState("");
const onSubmit = useContext(RoomContext);
const navigate = useNavigate();
const handleSubmit = async (e) => {
e.preventDefault(); // prevents page reload
if (room !== "") {
// socket.emit("join_room", room);
setRoom(e.target.value);
onSubmit(e.target.value);
navigate("/live_chat");
}
};
URLs: before: http://localhost:3000/join_room
after: http://localhost:3000/join_room?
expected: //localhost:3000/live_chat
Here is how I've written the routes:
<RoomContext.Provider value={{ room, setRoom }}>
<Routes>
<Route path="/" element={<Login />} />
<Route path="/signup" element={<Signup />} />
<Route path="/join_room" element={<JoinRoom />} />
<Route
path="/live_chat"
element={
<ProtectedRoute>
<LiveChat />
</ProtectedRoute>
}
/>
</Routes>
</RoomContext.Provider>
There are no errors so I am not sure on how to solve this.