reactjs - REACT js 中的 props 和 state 混淆
问题描述
在这里有一个简单的问题。我现在有 2 个类,AExample 类和 BExample 类。所以现在,我在 AExample 类的渲染部分创建了一个 BExample 类的实例。我已在渲染部分的 A 类中添加了此代码。
<BExample
state= {this.state}
/>
所以在 BExample 类中,当我尝试
console.log(props);
在道具价值中,我得到了状态的价值。问题是我现在很困惑,是 BExample 还是 AExample 的状态?你们的状态值代表哪个类?如果是,为什么会这样?
解决方案
看,state
ofAExample
现在被设置为组件命名的state
道具BExample
。
任何作为属性的组件的 prop 在 react 中都被称为附加组件的 prop。
所以,
<BExample
state= {this.state}
/>
在上面的例子{this.state}
中是 the 的状态AExample
但是state
是 的一个 prop BExample
。这就是为什么当您记录时props
它显示的状态值为AExample
.
推荐阅读
- java - 在 JPA 解决方法中使用 registerFunction
- python - KivyMD MDNavigationRail,按图标
- node.js - 尝试使用 LRO 运行云函数
- reactjs - 调试反应第三方依赖
- c++ - 从列表 C++ 创建队列
- meson-build - 在 Meson 中将 @BASENAME@ 与 custom_target() 的 install_dir 一起使用
- excel - 在完成之前在宏之后运行宏列表
- typescript - 在 TypeScript 中通过字符串设置枚举类型的变量
- r - 如何使用 dplyr 创建一个以 R 中其他两列的出现为条件的新列?
- python - 在 Python 中使用 Excel 列并定义哪个是 x 和 y