It's been a while since I've used react router so bare with me. I'm trying to understand how to send a prop down while still using react router's history to change the URL. That sounds trivial, but I'm having a tough time understanding this. I have a bare minimum setup for the routing just enough to initialize the history object. From there, I'm trying to build out the app as normal to send props down without using redux.
index.js
ReactDOM.render(
<Router>
<Route exact path='/'>
<App />
</Route>
</Router>
,document.getElementById('root')
app.js
...
let history = useHistory();
...
return (
<div className="main-wrapper">
<header>
<Header />
</header>
<main>
<Route path='/'>
<Search collectWords={collectWords} searchWords={searchWords} />
</Route>
<Route path='/search-results'>
<SearchResults people={people} />
</Route>
</main>
</div>
);
When a user performs a search, this function is called, but this is where things break for me. I get a blank screen with no props, but the URL does change.
const searchWords = (e) => {
if (e) {
e.preventDefault();
restAPI.getPeople(words)
.then(response => {
setPeople(response.data.data);
history.push('/search-results');
})
.catch(error => { throw new Error(error) });
}
}
Routers and this is a no-go. Remove the second one, you only need oneRouterper app but you can use manyRoutes.Appcomponent with an exact/route - that means that any route except the root will not render the App component..