首页 > 编程技术 > js

解决React报错useNavigate() may be used only in context of Router

发布时间:2022-12-4 14:29 作者:Borislav Hadzhiev

总览

当我们尝试在react router的Router上下文外部使用useNavigate 钩子时,会产生"useNavigate() may be used only in the context of a Router component"警告。为了解决该问题,只在Router上下文中使用useNavigate 钩子。

下面是一个在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);
// 

原文出处:https://juejin.cn/post/7129514060642516999

标签:[!--infotagslink--]

您可能感兴趣的文章: