reactjs - React JS 如何在 Hook 中的页面之间传递数据(useEffect)
问题描述
我试图将一个数据对象传递到另一页,但我无法获取第二页上的数据。下面的代码是我正在使用
第一页传递 LINK 中的数据
<Link to={{ pathname: `${path}/users/${organization.id}`,
data: organization
}}> <img src={config.s3Bucket + "/" + organization.logo} />
</Link >
在这里,我在“数据”参数中传递对象
第二页
import React, { useState, useEffect } from 'react';
function UserList({ history, match }) {
const { path } = match;
const { id } = match.params;
const [organization, setOrganization] = useState(null);
// const { data } = this.props.location
useEffect(() => {
// console.log(location.data);
}, []); } export { UserList };
我已经尝试了“location.data”和“this.props.location”,但我无法获取数据,请帮我解决这个问题。
解决方案
你可以这样做
<Link to={{ pathname: `${path}/users/${organization.id}`, state: organization}}>
<img src={config.s3Bucket + "/" + organization.logo} />
</Link >
并在第二页
import React, { useState, useEffect } from 'react';
import {useLocation} from 'react-router-dom';
function UserList({ history, match }) {
const { path } = match;
const { id } = match.params;
const [organization, setOrganization] = useState(null);
const { state } = useLocation();
useEffect(() => {
console.log(state);
}, []);
}
export { UserList };
推荐阅读
- php - 如果数据值为空,如何不显示字符串数据?php
- java - Spring Boot、Gradle 和非 Maven 依赖项
- php - 您如何在 hasMany 关系中更改 laravel 模型?
- java - 在任务栏 Java 中创建 Infobubble
- java - ManyToOne OneToMany 双向:从参考表中检索数据
- php - Laravel 工厂如何从你的数据库种子文件中调用它们
- python - Python矩阵乘法索引交换内存使用示例
- html - HTML && CSS 如何删除 div 的未使用位置
- python - 如何从 pyqt5 groupbox 发出可以动态更新的信号
- python - 具有多个条件和值重新分配的列表理解