React报错之useNavigate() may be used only in context of Router
正文从这开始~
总览
当我们尝试在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);
// ️ wrap App in Router
root.render(
<Router>
<App />
</Router>
);
useNavigate
现在,你可以在App.js文件中使用useNavigate钩子。
// App.js
import React from 'react';
import {
useNavigate,
} from 'react-router-dom';
export default function App() {
const navigate = useNavigate();
const handleClick = () => {
// ️ navigate programmatically
navigate('/about');
};
return (
<div>
<button onClick={handleClick}>Navigate to About</button>
</div>
);
}
会发生错误是因为useNavigate钩子使用了Router组件提供的上下文,所以它必须嵌套在Router里面。
用Router组件包裹你的React应用程序的最佳位置是在你的
index.js文件中,因为那是你的React应用程序的入口点。
一旦你的整个应用都被Router组件所包裹,你可以随时随地的在组件中使用react router所提供的钩子。
Jest
如果你在使用Jest测试库时遇到错误,解决办法也是一样的。你必须把使用useNavigate钩子的组件包裹在一个Router中。
// App.test.js
import {render} from '@testing-library/react';
import App from './App';
import {BrowserRouter as Router} from 'react-router-dom';
// ️ wrap component that uses useNavigate in Router
test('renders react component', async () => {
render(
<Router>
<App />
</Router>,
);
// your tests...
});
useNavigate钩子返回一个函数,让我们以编程方式进行路由跳转,例如在一个表单提交后。
我们传递给navigate函数的参数与<Link to="/about">组件上的to属性相同。
replace
如果你想使用相当于history.replace()的方法,请向navigate函数传递一个配置参数。
// App.js
import {useNavigate} from 'react-router-dom';
export default function App() {
const navigate = useNavigate();
const handleClick = () => {
// ️ replace set to true
navigate('/about', {replace: true});
};
return (
<div>
<button onClick={handleClick}>Navigate to About</button>
</div>
);
}
当在配置对象中将replace属性的值设置为true时,浏览器历史堆栈中的当前条目会被新的条目所替换。
换句话说,由这种方式导航到新的路由,不会在浏览器历史堆栈中推入新的条目。因此如果用户点击了回退按钮,并不会导航到上一个页面。
这是很有用的。比如说,当用户登录后,你不想让用户能够点击回退按钮,再次回到登录页面。或者说,有一个路由要重定向到另一个页面,你不想让用户点击回退按钮从而再次重定向。
你也可以使用数值调用navigate 函数,实现从历史堆栈中回退的效果。例如,navigate(-1)就相当于按下了后退按钮。
React报错之useNavigate() may be used only in context of Router的更多相关文章
- react 报错的堆栈处理
react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...
- sqlplus 连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0
sqlplus 连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0 问题描述: 使用sqlplus客户端登录数据库,报 ...
- 部署Maven项目到tomcat报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener【转】
部署Maven项目到tomcat报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi ...
- eclipse使用maven,启动工程tomcat报错:java.lang.ClassNotFoundException: org.springframework.web.context.Contex
maven是个不错的管理jar包工具,但是我们在eclipse使用maven时,总是遇上这样那样的问题,比如今天,我编译工程,启动过后,tomcat报错:java.lang.ClassNotFound ...
- sqlplus连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0解决
sqlplus连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0解决 sqlplus 连接数据库报错SP2-0642: ...
- React报错 :browserHistory doesn't exist in react-router
由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...
- react报错 TypeError: Cannot read property 'setState' of undefined
代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...
- React报错之Cannot find name
正文从这开始~ .tsx扩展名 为了在React TypeScript中解决Cannot find name报错,我们需要在使用JSX文件时使用.tsx扩展名,在你的tsconfig.json文件中把 ...
- React报错之Cannot find namespace context
正文从这开始~ 总览 在React中,为了解决"Cannot find namespace context"错误,在你使用JSX的文件中使用.tsx扩展名,在你的tsconfig. ...
随机推荐
- 好客租房9-jsx的学习目标
1能够知道什么是jsx 2能够使用jsx创建react元素 3能够在jsx使用javascript表达式 4能够使用jsx的条件渲染和列表渲染 5能够给jsx添加样式 jsx的基本使用 jsx中使用j ...
- CF1625D - Binary Spiders[trie树优化dp]
官方题解 题意:给数列a[],选择尽量多的数满足任意两个异或起来<=k 1625D - Binary Spiders 思路:首先,将数列排序得到,然后升序取得的值的任意两个最小值为相邻两个异或的 ...
- PostgreSQL 13支持增量排序(Incremental Sorting)
PostgreSQL 13支持增量排序(Incremental Sorting) PostgreSQL 13一个重要的功能是支持增量排序,使用order by 时可以加速排序,SQL如下 select ...
- Python数据分析--Numpy常用函数介绍(9)--Numpy中几中常见的图形
在NumPy中,所有的标准三角函数如sin.cos.tan等均有对应的通用函数. 一.利萨茹曲线 (Lissajous curve)利萨茹曲线是一种很有趣的使用三角函数的方式(示波器上显示出利萨茹曲线 ...
- 七牛云创建存储空间并绑定自定义域名-https协议
七牛云创建存储空间并绑定自定义域名-https协议 一.准备 0.绑定自定义域名的前提:你起码拥有过一个备案过的域名[一级域名] 1.在七牛云创建一个存储空间 2.存储空间绑定自定义域名(cdn加速) ...
- 【Spring】AOP实现原理(二):Advisor获取
@EnableAspectJAutoProxy @EnableAspectJAutoProxy注解可以用来开启AOP,那么就从@EnableAspectJAutoProxy入手学习一下Spring A ...
- Markdown常见基本语法
标题 -方式一:使用警号 几个警号就是几级标题,eg: # 一级标题 -方式二: 使用快捷键 ctrl+数字 几级标题就选其对应的数字, eg: ctrl+2(二级标题) 子标题 -方式一: 使用星号 ...
- redis入门,linux安装
1.下载 https://redis.io/download 2.上传到linux服务器tools文件夹下 3.解压到安装目录 tar -zxf /app/redis/redis-5.0.4.tar. ...
- CSCMS代码审计
很久之前审的了. 文章首发于奇安信攻防社区 https://forum.butian.net/share/1626 0x00 前言 CSCMS是一款强大的多功能内容管理系统,采用php5+mysql进 ...
- BUUCTF-秘密文件
秘密文件 根据提示得知是属于文件被下载了,查看了下流量包直接过滤ftp包 这里看到有个RAR包存在,应该是隐写了 使用foremost分离即可 得到压缩包存在密码 默认四位纯数字爆破即可 flag{d ...