【发布时间】:2020-03-13 15:35:06
【问题描述】:
我对如何将数据从一个组件传输到另一个组件做出反应并苦苦挣扎。
我参考了一些教程和博客,但对我不起作用。
我有两个子组件,Body-content.jsx 和 Profile.jsx 和 1 个父组件 parent.jsx
我想将一些数据从Body-content.jsx 传输到Profile.jsx。
这是我的代码
Body-content.jsx
class BodyContent extends React.Component {
componentDidMount() {
this.getUserList()
}
getUserList(){
fetch('https://jsonplaceholder.typicode.com/users')
.then(result => {
return result.json();
}).then(data =>{
this.setState({
users : data
})
})
}
render() {
const user = this.state.users.map((userData, i) => (
<CardBody>
...some code here
<Button color='primary' onClick={e => this.viewProfile(userData)}>View Profile</Button>
</CardBody>
</Card>
));
return (
<>
<div>{user}</div>
</>
)
}
viewProfile = function (data) {
}
}
export default BodyContent;
profile.jsx
class Profile extends React.Component {
componentDidMount() {
}
render() {
return (
<>
<TopNav />
<main className="profile-page" ref="main">
<section>
//code goes here
</section>
</main>
</>
);
}
}
export default Profile;
【问题讨论】:
-
你想要什么?你的意思是把
this.state.users传给Profile.jsx -
是... this.state.users 有我想在 profile.jsx 中显示的用户详细信息
-
您需要
parent.jsx来加载数据并管理状态并将正确的数据传递给您的子组件。 -
@Andy 那是我做不到的
标签: javascript reactjs