简体   繁体   English

React.js:Function 从父组件传递到子组件未被调用

[英]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.

相关问题 在React.js中从父组件调用子组件函数 - Calling Child component function from Parent component in React.js 在React.JS中从子级调用父组件函数 - Call parent component function from child in React.JS 当放置在单独的文件中时,通过 React.js 中的 ref 从父组件调用时无法访问子函数 - Child function not accessible when called from parent component through ref in React.js when placed in separate files 如何在React.JS中将状态从子组件传递给父组件? - How to pass state from child component to parent in React.JS? 如何在 React.JS 中将数据从子组件传递到父组件? - How to pass data from child to parent component in React.JS? React.js-如何在子组件中实现一个功能,以从同一个父组件中卸载另一个子组件,并在其位置装载另一个组件? - React.js - How to implement a function in a child component to unmount another child from the same parent, and mount another component on it's place? React.js:从其父类调用子组件函数/方法 - React.js: Call a child component function/method from its parent class 从父组件 React JS 调用 map function 时如何更新子组件的 state - How to update state of child component when it get called in map function from parent component React JS 在React中没有从子组件传递给父组件的值 - Value not getting passed from child to parent component in React react.js-从父组件调用函数 - react.js - Call function from parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM