react location.state时不时接收不到参数的问题 Cannot read properties of undefined (reading ‘record‘)
·
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')));
更多推荐
所有评论(0)