简体   繁体   English

为什么我的 function 传递给孩子时出现无法读取属性道具错误?

[英]Why am I getting a cannot read property props error for my function passed to a child?

So I have a button which updates an array stored in my back-end, I need that button to also update the state of my front-end with the new array from the back-end.所以我有一个按钮来更新存储在我的后端的数组,我需要这个按钮来更新我前端的 state 与后端的新数组。 I am passing a function which changes the state of the parent to the button but when I try to call that function with this.props.function() I get a TypeError: Cannot read property 'props' of undefined.我正在传递一个 function ,它将父级的 state 更改为按钮,但是当我尝试调用该 function 时,无法使用 this.props.function()

I have another button which uses basically the same code(except this one I call the passed function inside another function which I bind to onClick) and works perfectly fine so I am confused as to why this one is not working.我有另一个按钮,它使用基本相同的代码(除了这个我在另一个 function 中调用传递的 function ,我绑定到 onClick)并且工作得很好,所以我很困惑为什么这个不工作。

Parent:家长:

//within constructor
this.onUpdateRooms = this.onUpdateRooms.bind(this);

//function
onUpdateRooms(list) {
    this.setState({roomList: list});
    console.log(this.state.roomList);
  }

//within render
<GenerateButton onUpdateRooms={this.onUpdateRooms}/>

Button按钮

//within the class declaration
constructor (props) {
        super(props)
    }

onCreateNewRoom() {
        const newRoomCode = GenerateRandomCode.TextCode(6).toUpperCase();
        console.log(newRoomCode);
        let temp;
        fetch('/api/newRoom', {
            method: "post",
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({roomCode: newRoomCode})
        })
        .then(res => res.json())
        .then(list => temp=list);

        this.props.onUpdateRooms(temp);
    }
//then i set the button's onClick={this.onCreateNewRoom}

I can't seem to figure out whats wrong.我似乎无法弄清楚出了什么问题。 I've been deleting and adding things for the last 2 hours.在过去的 2 个小时里,我一直在删除和添加内容。 Help would be greatly appreciated:)帮助将不胜感激:)

you also need to call bind on onCreateNewRoom您还需要在onCreateNewRoom上调用bind

constructor (props) {
        super(props);
    this.onCreateNewRoom = this.onCreateNewRoom.bind(this);
    }

you could also use arrow functions to avoid re-binding this and this kind of mistakes:您还可以使用箭头函数来避免重新绑定this错误和此类错误:

onCreateNewRoom = () => {

So this inside of onCreateNewRoom will be bound to the class and not the onClick handler function.因此onCreateNewRoom this内部将绑定到 class 而不是onClick处理程序 function。

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. bind() 方法创建一个新的 function,当调用它时,它的 this 关键字设置为提供的值,在调用新的 ZC1C425268E68385D1AB5074C17A94F1 时,给定的 arguments 序列在任何提供的序列之前。

In the constructor this is bound to the class.在构造函数中this绑定到 class。 When you call this.onCreateNewRoom.bind(this) you are setting this inside of the onCreateNewRoom to the value you pass as the first argument to bind .当您调用this.onCreateNewRoom.bind(this)时,您正在将onCreateNewRoom内部的this设置为您作为第一个参数传递给bind的值。 (Which is the this bound to the class and contains props ). this是绑定到 class 并包含props的 this )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现“无法读取null的属性样式”错误? - Why I am getting “cannot read property style of null” error? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 为什么会出现“ TypeError:无法读取未定义的属性” props”(反应路由器,React-Redux)? - Why am I getting 'TypeError: Cannot read property 'props' of undefined' (react-router, React-Redux)? 无法读取 null 错误的属性“innerHTML”。 我的脚本在正文的末尾,为什么我仍然收到此错误? - Cannot read property 'innerHTML' of null error. My script is at the end of the body, why am I still getting this error? 尝试 map 我的道具时,我收到无法读取未定义错误的“练习”属性 - I'm getting a Cannot read property of 'exercises' of undefined error when trying to map my props 传递给 onChange 方法的子组件时出现“无法读取未定义道具”的错误 - Getting error of "Cannot read props of undefined" when being passed to child component for onChange method 为什么我的脚本中出现“未捕获的TypeError:无法读取null的属性&#39;nodeName&#39;”? - Why am I getting “Uncaught TypeError: Cannot read property 'nodeName' of null” in my script? 尝试访问嵌入的Google地图时,为什么会出现无法读取未定义的属性“ getCenter”的问题? - Why am I getting Cannot read property 'getCenter' of undefined when trying to access my embed google map? 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM