首页 > 解决方案 > REACT js 中的 props 和 state 混淆

问题描述

在这里有一个简单的问题。我现在有 2 个类,AExample 类和 BExample 类。所以现在,我在 AExample 类的渲染部分创建了一个 BExample 类的实例。我已在渲染部分的 A 类中添加了此代码。

<BExample
state= {this.state}
/>

所以在 BExample 类中,当我尝试

console.log(props);

在道具价值中,我得到了状态的价值。问题是我现在很困惑,是 BExample 还是 AExample 的状态?你们的状态值代表哪个类?如果是,为什么会这样?

标签: reactjsstatereact-props

解决方案


看,stateofAExample现在被设置为组件命名的state道具BExample

任何作为属性的组件的 prop 在 react 中都被称为附加组件的 prop。

所以,

<BExample
state= {this.state}
/>

在上面的例子{this.state}中是 the 的状态AExample但是state是 的一个 prop BExample。这就是为什么当您记录时props它显示的状态值为AExample.


推荐阅读