我有以下对象(在其中一些点操作之后) 我称它为props.stuff, 给定:数据作为
从父组件传递到子组件//Parent
componentDidMount() {
this.getData();
}
async getData() {
const res = await axios.get(
"{link here}"
);
const { data } = await res;
this.setState({ stuff: data});
}
<Child props={this.state.stuff} />
所以,在我做console.log(props.stuff)
的“孩子”中:
props.stuff= {
"date":"November 14",
"status":"PENDING",
"id":"146859",
"invoice_number":"123685479624",
"amount":"950.00",
"currency":"$"
}
但是尝试访问props.stuff.date(或诸如props.stuff。 {element} 之类的东西)会给我 undefined 。 我在做什么错了?
注意::通过执行console.log(JSON.stringify(props.stuff)),我得到了下面的字符串,所以这正是它的本质。
答案 0 :(得分:-2)
所以我终于找到了解决方案。实际上,道具是从父组件传递到子组件的,而在父组件中,道具是通过API调用获取的。因此道具的初始值是不确定的。
在父组件中,编写:
{props.stuff && <Child stuff={props.stuff} />}
这可以确保仅在道具存在时通过道具,从而解决了问题