原文链接:https://bobbyhadz.com/blog/react-usenavigate-may-be-used-only-in-context-of-router[1]
作者:Borislav Hadzhiev[2]
正文从这开始~
总览
当我们尝试在react router的Router上下文外部使用useNavigate
钩子时,会产生"useNavigate() may be used only in the context of a Router component"警告。为了解决该问题,只在Router上下文中使用useNavigate
钩子。
usenavigate-may-be-used-only-in-the-context-of-router.png
下面是一个在index.js
文件中将React应用包裹到Router中的例子。
// index.js
import {createRoot} from 'react-dom/client';
import App from './App';
import {BrowserRouter as Router} from 'react-router-dom';
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);
//