I'm trying to extract data from the ColorChild component using the setNewColor function setNewColor which I pass to the props

 class Color extends React.Component { constructor(props) { super(props); this.state={ currentColor: null } this.setNewColor = this.setNewColor.bind(this, true) } renderColors(color, index) { return ( //ПЕРЕДАЮ ФУНКЦИЮ setNewColor В КОМПОНЕНТ ColorChild <ColorChild key={index} color={color} setNewColor={this.setNewColor} /> ) } setNewColor(color){ console.log('setNewColor', color.name); } render() { const {color} = this.props return ( <div className="product-item__block"> <span className="product-item__title">Color</span> <div className="product-set"> {color.map((color, index) => this.renderColors(color, index))} </div> </div> ); } } 

 class ColorChild extends React.Component { constructor(props) { super(props); this.sendColor = this.sendColor.bind(this) } sendColor(color){ //ПЕРЕДАЮ ДАННЫЕ В РОДИТЕЛЯ this.props.setNewColor(color) } render() { const {color} = this.props return ( <span onClick={() => this.sendColor(color)} style={{backgroundColor : color.color}} className={`product-set__item color ${(color.active ? 'active': '')}`}/> ); } } 

In component ColorChild use function, it is executed, but the input parameter is undefined.

PS I do by analogy http://jsbin.com/tipixiy/edit?js,output

  • What is transmitted color ? MB did you want to display color.color ? - exvayn
  • @exvayn color is an object, but I think that is not the point. It is not transmitted - Alexander Alekseev
  • setNewColor undefined - is console.log? - exvayn
  • @exvayn yes, this is console.log in the parent - Alexander Alekseev
  • one
    I understood! problem in this.setNewColor = this.setNewColor.bind(this, true) . It is necessary this.setNewColor = this.setNewColor.bind(this) . true - set as the first default parameter. And the transmitted color comes 2nd. - exvayn

0