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
color? MB did you want to displaycolor.color? - exvayncoloris an object, but I think that is not the point. It is not transmitted - Alexander AlekseevsetNewColor undefined- is console.log? - exvaynthis.setNewColor = this.setNewColor.bind(this, true). It is necessarythis.setNewColor = this.setNewColor.bind(this).true- set as the first default parameter. And the transmittedcolorcomes 2nd. - exvayn