react当从A页面history.push('/ksgl', { record });到另一个B页面  

const location = useLocation();
const { record } = location.state; 

用此接收,但有时会出现 Cannot read properties of undefined (reading 'record')  但是数据确实存在 ,第一次 A页面(强制刷新)history.push的时候B页面就可以接收到,有类似情况 以下解决方法:

使用 URL 参数

A页面 传递参数

history.push(`/ksgl?record=${encodeURIComponent(JSON.stringify(record))}`);

B页面 解析参数

  const location = useLocation();
  const searchParams = new URLSearchParams(location.search);
  const record = JSON.parse(decodeURIComponent(searchParams.get('record')));

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐