[英]React.js: Function passed from parent component to child component not getting called
I'm a beginner and trying to learn React.我是初学者,正在尝试学习 React。 I have a parent component with a boolean in its state that I change with a function defined in its class called rerender().
我有一个父组件,其 state 中有一个 boolean,我将其更改为 class 中定义的 function,称为 rerender()。 I pass this function down into a child component's props like so:
我将这个 function 传递给子组件的道具,如下所示:
<Todolist
color={color}
name={name}
items={items}
addListItem={this.addListItem}
rerender={() => this.rerender}
/>
I have a console.log in that function to check if it is being called.我在那个 function 中有一个 console.log 来检查它是否被调用。 I call it in the child component like this:
我在子组件中这样调用它:
this.props.rerender();
I'm certain that the onChange event which calls this.props.rerender()
in the child component is firing, so what are some possible reasons as to why it isn't being called?我确定在子组件中调用
this.props.rerender()
的 onChange 事件正在触发,那么它没有被调用的可能原因是什么?
this will not work hence the function is not called, 2 options:这将不起作用,因此不会调用 function,有 2 个选项:
rerender={() => this.rerender()}
or要么
rerender={this.rerender}
the latter one gets any arguments passed to it, but the first one won't get any arguments.后一个得到传递给它的任何 arguments,但第一个不会得到任何 arguments。
You may need to bind
the this.renderer
if it's not defined as an arrow function.如果
this.renderer
未定义为箭头 function,您可能需要bind
它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.