{"id":7774,"date":"2023-05-04T07:25:38","date_gmt":"2023-05-04T07:25:38","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=7774"},"modified":"2023-08-29T05:53:18","modified_gmt":"2023-08-29T05:53:18","slug":"usehistory-was-not-found-in-react-router-dom","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react\/usehistory-was-not-found-in-react-router-dom","title":{"rendered":"useHistory was not found in React-router-dom"},"content":{"rendered":"<p>react-router-dom v6 has replaced the useHistory() with useNavigate(). Here is the usage of useNavigate.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-linenumbers=\"false\">import { useNavigate } from \"react-router-dom\";<\/pre>\n<p>Inside your functional component,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-linenumbers=\"false\">const navigate = useNavigate(); \r\nconst onClick = () =&gt; { navigate(\u2018\/path) }<\/pre>\n<p>Following are the relevant methods of useNavigate hook:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-linenumbers=\"false\">\u2022 Replace history.push('\/path') with navigate('\/path') \r\n\u2022 Replace history.replace('\/path') with navigate('\/path', { replace: true }) \r\n\u2022 Replace history.push({ pathname: '\/path', state: yourData, }) with navigate('\/path', { state: yourData })<\/pre>\n<p>If you\u2019re not planning to upgrade to v6 and lots of your files are already in react-router v5 then you can try migrating to the latest minor version of v5 by<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-linenumbers=\"false\">npm install react-router-dom@\"&lt;6.0.0\"<\/pre>\n<p><span style=\"font-weight: 400;\">Then, over time, you can migrate it to the latest version by following their <\/span><a href=\"https:\/\/reactrouter.com\/en\/main\/upgrading\/v5\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">official guidelines<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>react-router-dom v6 has replaced the useHistory() with useNavigate(). Here is the usage of useNavigate. import { useNavigate } from &#8220;react-router-dom&#8221;; Inside your functional component, const navigate = useNavigate(); const onClick = () =&gt; { navigate(\u2018\/path) } Following are the relevant methods of useNavigate hook: \u2022 Replace history.push(&#8216;\/path&#8217;) with navigate(&#8216;\/path&#8217;) \u2022 Replace history.replace(&#8216;\/path&#8217;) with navigate(&#8216;\/path&#8217;, { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8331,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7774","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7774"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=7774"}],"version-history":[{"count":16,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7774\/revisions"}],"predecessor-version":[{"id":8333,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7774\/revisions\/8333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8331"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=7774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=7774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=7774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}