[英]Props returns undefined, but I can see its value in the console
I have a tiny, but frustrating bug. 我有一个小但令人沮丧的错误。 I have a parent component that passes down a prop to its child: 我有一个将组件传递给它的孩子的父组件:
const { door } = this.props || []
...
{this.state.showModal ? <ClusterModal door={door} /> : null}
And when I want to access it in my child component: this.props.door
. 当我想在子组件中访问它时: this.props.door
。
Also the door
props has a key called doors
that I want to access. 另外, door
道具还有一个我想访问的名为doors
的钥匙。 So I tried: console.log(this.props.door.doors)
and my console gives me: 所以我尝试了: console.log(this.props.door.doors)
,我的控制台给了我:
Which is great! 太好了! However React tells me this: 但是React告诉我这一点:
Cannot read property 'doors' of undefined 无法读取未定义的属性“门”
Sorry if this is a bad explained question, but I can't figure this out. 抱歉,这是一个不好解释的问题,但我无法弄清楚。
Thanks for reading! 谢谢阅读!
Here's the parent: 这是父母:
const { door } = this.props
const ifCluster = (
<div>
{door.type === 'cluster' ? (
<div className="door-flex-item-2">
<a
className="sub-title-text-container"
onClick={() => this.toggleModalButton()}
>
Hejsan
</a>
{this.state.showModal ? this.props.door ? (
<ClusterModal door={door} />
) : null : null}
</div>
) : null}
</div>
)
And here's the child: 这是孩子:
render() {
// const { door } = this.props || []
const customers = this.props.customers || []
const keyedCustomers = _.keyBy(customers, '_id')
const deliveries = this.props.deliveries || []
const keyedDeliveries = _.keyBy(deliveries, '_id')
console.log(this.props.door)
const clusterDoors = this.props.door.doors.map((clusterDoor, i) => {
console.log(clusterDoor)
I noticed that the first time I log this.props.door
I get the data, but the second time it gets undefined
for some reason. 我注意到,第一次登录this.props.door
我得到了数据,但是第二次由于某种原因,它变得undefined
。
Store the prop values in state and check the values are same as in your props. 将prop值存储在状态中,并检查值是否与prop中的值相同。 Do it in ComponentWillReceiveProps(nextProps) method. 用ComponentWillReceiveProps(nextProps)方法执行。
Add condition to your component like this 像这样向组件添加条件
(this.props.door) ? <ClusterModal door={door} /> : <Div/>
So, it will only render component once it has value within door 因此,只有在门内有价值后才渲染组件
Edit : 编辑 :
{
(this.state.showModal)
? (this.props.door)
? <ClusterModal door={door} />
: null
: null
}
or you can use function as well 或者你也可以使用功能
clusterModel = () => { // change according to requirement
if (this.state.showModal) {
if (this.props.door) {
<ClusterModal door={door} />
}
}
}
The only way that you will see the error: 您将看到错误的唯一方法:
Cannot read property 'doors' of undefined 无法读取未定义的属性“门”
Is that the this.props.door.doors
from inside of ClusterModal
component returns undefined at the door
property. 是来自ClusterModal
组件内部的this.props.door.doors
在door
属性处返回未定义。
And this is the case of const { door } = this.props || []
这就是const { door } = this.props || []
const { door } = this.props || []
setting door to undefined. const { door } = this.props || []
将门设置为未定义。 This happens because the right hand side this.props || []
发生这种情况是因为右侧this.props || []
this.props || []
evaluates to []
and the left side fails to do the object deconstruction because you have an array instead of an object with the door
key. this.props || []
为[]
,并且左侧无法进行对象解构,因为您拥有数组,而不是带有door
钥匙的对象。
A quick fix should be to do: 一个快速修复方法是:
const { door } = this.props || { door: { doors: []} }
Then inside of your ClusterModal you will NOT get that error. 然后,在您的ClusterModal内部,您将不会收到该错误。
Here you have the rules of object and array destructuring . 在这里,您具有对象和数组解构的规则。
Please check this because const { door } = this.props || []
请检查此内容,因为const { door } = this.props || []
const { door } = this.props || []
does not makes sense. const { door } = this.props || []
没有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.