Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Pass function to child component

Tags:

I want to pass a function to a child component but I'm getting this error.

Invalid value for prop passedFunction on <div> tag.

class Parent extends Component {     passedFunction(){}     render() {       <Child passedFunction={this.passedFunction}/>     } }  class Child extends Component {     render() {         <div onClick={this.props.passedFunction}></div>     } } 

Basically what I'm trying to do.

var ReactGridLayout = require('react-grid-layout');

var MyFirstGrid = React.createClass({ passedFunction:function(){}   render: function () {     return (       <ReactGridLayout className="layout" cols={12} rowHeight={30} width={1200}>         <div key="a" data-grid={{x: 0, y: 0, w: 1, h: 2, static: true}}>a</div>         <div key="b" data-grid={{x: 1, y: 0, w: 3, h: 2, minW: 2, maxW: 4}}>b</div>         <div key="c" data-grid={{x: 4, y: 0, w: 1, h: 2}} passedFunction={this.passedFunction}>c</div>       </ReactGridLayout>     )   } }); 

It seems it was introduced in React v16. Therefore, what is the correct way to pass a function from parent to child?

like image 718
Lun Zhang Avatar asked Jan 23 '18 17:01

Lun Zhang


People also ask

How do you pass a function to a child component React?

Define the function in the parent component. Pass it as a prop to the child component, e.g. <Child handleClick={handleClick} /> . Use the function in the child component.

How do you pass a function from one component to another in React JS?

For passing the data from the child component to the parent component, we have to create a callback function in the parent component and then pass the callback function to the child component as a prop. This callback function will retrieve the data from the child component.

What is the purpose of a function as a child in React?

A Function as child component is a pattern that lets you pass a render function to a component as the children prop so you can change what you can pass as children to a component.


2 Answers

Instead of having to bind your function in the constructor of the parent Class, you can use an arrow function to define your method so it is lexically bound using an arrow function

class Child extends Component {     render() {         <div onClick={this.props.passedFunction}></div>     } }  class Parent extends Component {     passedFunction = () => {}     render() {       <Child passedFunction={this.passedFunction}/>     } } 

To Account for older version support of Javascript:

class Child extends Component {     render() {         <div onClick={this.props.passedFunction}></div>     } }  class Parent extends Component {     constructor() {         this.passedFunction = this.passedFunction.bind(this)     }      passedFunction() {}     render() {       <Child passedFunction={this.passedFunction}/>     } } 
like image 152
Benjamin Charais Avatar answered Sep 19 '22 10:09

Benjamin Charais


You are missing bind on the Child component.

this.props.passedFunction.bind(this) 
like image 37
Ynahmany Avatar answered Sep 17 '22 10:09

Ynahmany