前言:一些很簡單的頁面例如從列表頁面進入詳情頁面或者編輯頁面會將id放入到url中,然后在對應的頁面url中獲取id,如果參數過多就會使url變得很長而已很模糊,另外用戶還可以手動對參數進行修改,這樣極其不安全,有沒有什么方法讓用戶看不到參數,答案肯定是有的,接下來我們就探討一下。
一、傳遞參數
- 引入
useHistory
import { useHistory } from 'react-router-dom';
- 獲取實例
history
const history = useHistory()
- 頁面跳轉時
url是要跳轉的目標頁面地址
parameter是要在B頁面使用的參數一般是一個Object
history.push(url, parameter);
二、接收參數
- 引入
useLocation
import { useLocation } from 'react-router-dom';
- 獲取實例
location
const location = useLocation()
- 使用參數
location.state[參數名]
至此本篇文章就已經介紹完了,如果錯誤歡迎大家指正。