I am using React router dom v6, and the "useHistory" method is not available in the React router v6 version.
Please suggest me how I can implment below code with React Router com v6 version
history.listen(onParentNavigate);
export default () => {
const ref = useRef(null);
const location = useLocation();
const navigate = useNavigate();
const history = useHistory();
useEffect(() => {
const { onParentNavigate } = mount(ref.current, {
onNavigate: ({ location }: any) => {
navigate(location.pathname);
},
});
// PLEASE SUGGEST ME HOW I CAN IMPLEMENT THIS WITH REACT ROUTER V6 VERSION
history.listen(onParentNavigate);
}, []);
return (
<div ref={ref} />
)
}
2
Answers
I think you can refer to this post. It seems that you need to use
useNavigate
instead ofuseHistory
Issue
React-Router-DOM 6 doesn’t export any
useHistory
hook nor surface thehistory
object.Solution
Data Router
If you are using a Data Router (see Picking a Router) you can subscribe to the router.
Example:
Regular Non-Data Router
If you are not using any Data Router then you’ll need to use the
HistoryRouter
and a customhistory
object.Example: