I'm starting to use React hooks in my application, but it seems like there's hurdle after hurdle to figure out. I'm not sure if it's because of the learning curve or if dependencies aren't prepared to use this new implementation. In any case..
Before hooks, we used HOC's to connect into a specific API's whether it be Redux, React-Router, etc.. In my case, I want to get access to my React-Router properties (history, location).
Normally it would be as simple as doing this as the bottom of the file:
export default withRouter(SomeComponent);
But now with hooks, I'm not sure how to get access to this data.
How would I get access to this same type data from react-router with the new React hooks?
As per the comment by Tim Moses on the other answer, react-router now has hooks for this. Sample code has been pulled from the React Hooks docs:
import { useHistory } from "react-router-dom";
function HomeButton() {
let history = useHistory();
function handleClick() {
history.push("/home");
}
return (
<button type="button" onClick={handleClick}>
Go home
</button>
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With