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

243
Views
how do i pass a state in a route element in react-router?
...
import { useNavigate, NavigateFunction } from "react-router";
...

function Form(): JSX.Element {
  const navigate: NavigateFunction = useNavigate();
  const [country, setCountry] = useState<string>(" ");

  
  const handleChange = (e: ChangeEvent<HTMLInputElement>): void => {
    setCountry(e.target.value);
  };

  const handleSubmit = (e: FormEvent<HTMLFormElement>): void => {
    e.preventDefault();
    navigate("/country");
  };



  return (
    <Box component="form" onSubmit={handleSubmit}>
      <input
        type={"text"}
        required
        placeholder="Enter Country"
        value={country}
        onChange={handleChange}
      ></input>
      <Button variant={"contained"} type="submit">
        Search
      </Button>
    </Box>
  );
}
export default Form;

this is my index.tsx file:

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<App />} />
        <Route path="/country"  element={<CountryDetails   />} />
      </Routes>
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById("root")
);

this is my CountryDetails.tsx file:

const CountryDetails = (): JSX.Element => {
    const country: string | undefined = useParams().name;

    
    return (

       
        <Box>
           hi ${country}
        </Box>
    )
}
export default CountryDetails;

I want to send the country variable from Form.tsx to CountryDetails.tsx file. How do I send it. I am doing the route operations in index file. I tried to send it as props, as state also, even used link to instead of navigate function. Used history.push too. Please help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Can you try this?

<form onSubmit={() => navigate('/country', { state={ country }, replace=true })}>
  {...}
</form>

const params = useParams()
const { state } = params;
// I think country inside 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!