Suppose there is a route like <base-url>/search
the route is defined like
import { Link, BrowserRouter as Router, Route } from "react-router-dom";
import { useHistory, useLocation } from "react-router-dom";
...
<Router>
...
<Route path="/search/:searchInput">
<Search />
</Route>
</Router>
There is also a textbox component which uses a useContext
hook and contextprovider, and onChange
it keeps the current value of the textbox input which is used by the <Search/>
component
how do you change the path name in real time to the name of the search input? if the textbox is empty it should default to <base-url>/search
for example if you type 'lil' in the textbox, the current route or pathname will redirect / render simultanously to <base-url>/search/lil
if there are multiple words or space in the textbox such as 'lil red', the current path will render immediately to <base-url>/search/lil%20red
do i need to use react <Link/>
?
related thread How to update the url of a page using an input field?, react-router - how change the url
EDIT
import SearchContext from "../Search/context"
const Search = () => {
const context = useContext(SearchContext)
// context.searchInput is the value of the textbox provided by context.provider
useEffect(() => {}, [])
return (...)
}
export default Search