//Navbar.jsx
const [query, setQuery] = useState("")
const handleChange = (e) => {
setQuery(e.target.value) }
.
.
.
<input value={query} id="query" onChange={handleChange} />
Now my question is, I have a separate route ("/search/:query", component is Search.jsx). How do I send the ‘query’ state from the navbar to this route so that I can extract the query from the params and use it afterwards?
Basically it’s a movie site and I have a search bar at the home page (in navbar) and a separate page for searching too but I want the search parameter in home page/navbar to be sent to that search page route for params extraction
I thought of prop sending but that wouldn’t work since I’m sending data to the params of the url, so I’m pretty clueless
I am using react-router
2
Answers
Of course you can use props. Use this in your search.jsx page:
now with a simple code you can get the data you want:
if this doesn’t work for you, try this:
-> Follow my code :-
->Navbar.jsx :-
-> In your Search.jsx component,you can access this query parameter using React router useParams hooks.
-> Search.jsx :-