reactjs - 在 react 中访问 this,然后 this.props 将 props 转换为空对象
问题描述
我在 componentDidMount() 中使用 console.log(this),我可以看到格式正确且包含数据的所有内容(props.betAmounts)。但是,在同一个 componentDidMount() 中,当我 console.log(this.props.betAmounts) 我得到一个空对象...
这是一个带有显式问题的 codeSandBox(在控制台中可见)。
https://codesandbox.io/s/726jjqozzq
感谢您的帮助 !
解决方案
由于您是setState
从您的内部调用,componentDidMount
您将看到中间状态,因此在您的第一次渲染时,您betAmounts
将{}
来自您的App
状态,因此您将看到空对象。
componentDidMount
有关于此的文档
Calling setState() in this method will trigger an extra rendering, but it will happen before the browser updates the screen. This guarantees that even though the render() will be called twice in this case, the user won’t see the intermediate state.
这意味着由于componentDidMount
只调用一次,它将显示您的空对象,在第二次传递(来自额外触发的渲染)时,它将是您期望的对象。
推荐阅读
- amazon-web-services - 有没有办法以编程方式列出 AWS API Gateway 上部署的 API?
- r - R中的Webscraping来自Dataframe
- corda - Corda 交易基本问题
- java - 使用 Android Studio 的 PUT 请求
- python - 如何在 spyder 中导入和打印单独的 txt 文件?
- python - pipenv 未安装外部包的依赖项:ModuleNotFoundError
- mysql - nodejs mysql连接池在每个require语句后不断重新初始化
- java - 如何有效地将 2 种不同类型的多维集合转换为多维列表
- python - Raspberry Pi 未在 Pyhton 中通过串行通信正确向设备发送指令
- vb.net - 在 vb.net 中将 ByRef 变量设置为存储的 proc 输出