简体   繁体   English

道具返回未定义,但我可以在控制台中看到其值

[英]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: 我有一个将组件传递给它的孩子的父组件:

Parent: 上级:

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! 谢谢阅读!

Edit 编辑

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.doorsdoor属性处返回未定义。

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.

相关问题 密码值返回未定义,但是当使用console.log显示时,用户可以看到 - Password value returns undefined, but when console.log is used to display it, the user can see it 访问数组值返回“未定义”,但是我可以用console.dir()看到值吗? - accessing array values returns 'undefined', but I can see the values with console.dir()? 如何从值数组jQuery中查找键。 在这里,我想查看console.log(position)。 但其显示未定义 - how to find key from value array jquery. here i want to see the console.log(position). but its shows undefined 我可以在控制台日志中看到来自action(redux)的道具,但是无法在窗口上访问。 提供TypeError:无法读取未定义的prop&#39;rest&#39; - i can see props coming from action(redux) in console logs but can't access on window. Giving TypeError: Cannot read prop 'rest' of undefined 返回返回未定义但控制台返回值 - Return returns undefined but console returns value 属性未定义,但console.log可以看到它 - property undefined but console.log can see it 反应不渲染道具,但可以在console.log中看到 - React not rendering props but can see in console.log Axios在控制台上打印值,但返回未定义 - Axios prints value on console but returns undefined 调用javascript函数,值在控制台中返回为undefined - Calling a javascript function, value returns as undefined in the console axios promise值在Chrome控制台中返回undefined,在Firefox控制台中返回字符串 - axios promise value returns undefined in Chrome console and a string in Firefox console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM