引入:
import { useHistory } from 'react-router-dom';
下面那两个都要引入这个
传输数据:
//注意先引入上面的
const history = useHistory();
function data(){
history.push('需要数据的路由的url', { state:数据 })
}
接收数据:
注意先引入上面的
const history = useHistory();
//接收函数
useEffect(() => {
//做个判断别没收到报错
if (history.location.state) {
//打印看看
console.log(history.location.state.state)
}
}, [])
本文介绍了在React应用中如何使用`useHistory`钩子从一个组件向另一个组件传递数据。通过`history.push`方法将数据以状态(state)的形式添加到路由中,并在目标组件中利用`useEffect`监听`location.state`来接收并处理数据。
6558

被折叠的 条评论
为什么被折叠?



