在 React 中访问它,然后 this.props 将 props 转换为一个空对象

Accessing this in react, then this.props transform props in an empty object

I console.log(this) 在 componentDidMount() 中,我可以看到所有 (props.betAmounts) 格式正确且包含数据的内容。但是,在同一个 componentDidMount() 中,当我 console.log(this.props.betAmounts) 我得到一个空对象...

这是一个带有明确问题的 codeSandBox(在控制台中可见)。

https://codesandbox.io/s/726jjqozzq

感谢您的帮助!

由于您是从 componentDidMount 中调用 setState,您将看到中间状态,因此在您的第一个渲染中,您的 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 只被调用一次,它会显示你的空对象,在第二次传递时(来自额外触发的渲染)它将是你期望的对象。