Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
React Router Dom, pass data to a component with useNavigate

I'm using useNavigate to go to the component and need to pass data (a state) to this this ChatRoom component when I click a button. This component is on the route /chatroom. I'm using React Router Dom v6. I have read the documentation but I cannot find what I'm looking for.

export default function Home() {
  const [username, setUsername] = useState("");
  const [room, setRoom] = useState("");

  const navigate = useNavigate();

  const handleClick = () => {
    navigate("/chatroom");
  };

  return (<button
            type="submit"
            className="btn"
            onClick={() => {
              handleClick();
            }}
          >
            Join Chat
          </button>
)}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

1. Solution

You can pass data to the component that corresponds to /chatroom like this :

navigate('/chatroom', { state: "Hello World!" });

And consume with the help of useLocation this way :

import {useLocation} from "react-router-dom";
function ChatRoom() {
  const { state } = useLocation();
  return (
    <div>
      {state}
    </div>
  );
}
export default Chatroom;

If you wanna pass multiple state, use an object, like so:

navigate('/chatroom', { state: {id:1, text: "Hello World!"} });

2. Troubleshot

As you can try it here on this CodeSandbox, it works only if the passed data is called state.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!