網站首頁 編程語言 正文
解決React報錯useNavigate()?may?be?used?only?in?context?of?Router_React
作者:Borislav?Hadzhiev ? 更新時間: 2022-12-30 編程語言總覽
當我們嘗試在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)
就相當于按下了后退按鈕。
原文鏈接:bobbyhadz.com/blog/react-…
原文鏈接:https://juejin.cn/post/7129514060642516999
相關推薦
- 2022-03-14 IDEA 上傳文件 getRealpath("/upload)獲取不到文件上傳路徑問題
- 2023-01-07 Python個人博客程序開發實例用戶驗證功能_python
- 2022-05-25 Flutter實現掃二維碼功能_Android
- 2022-10-18 Qt基于TCP實現客戶端與服務端的連接_C 語言
- 2022-09-25 Redis數據的刪除策略
- 2022-10-03 Objective-C之Category實現分類示例詳解_IOS
- 2022-04-02 jquery實現邊框特效_jquery
- 2023-01-30 vite?+?react?+typescript?環境搭建小白入門教程_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支