且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

父组件更改时更新子组件

更新时间:2022-11-21 21:02:38

this.props.user = res.data.user; 

您无法分配道具.道具是从父母那里传递的.将用户设置为状态,然后将状态传递给您的子组件,如下所示:

You can't assign to props. Props are passed from a parent. Set the user in the state and pass the state to your child component like so:

<ABCD {...this.props} user={this.state.user} />

您现在可以在子组件中访问this.props.user.另外,也不需要this.forceUpdate().

In your child component you will now have access to this.props.user. Also the this.forceUpdate() will not be needed then.