1

I'm new to React and stuck trying assign parent's function to children created dynamically

class Row extends React.Component {

handleStateChange() {
    console.log(this); //just for test
}

render() {
    let notes = [],
        categoryId = this.props.rowNo;

    bonuses.forEach(function (bonus, i) {
        let id = 'cell_' + categoryId.toString() + (i + 1).toString();
        notes.push(<NoteCell bonus={bonus}
                             songName={id + '.mp3'}
                             id={id}
                             key={id}
                                        // that is the point
                             handleRowStateChange={this.handleStateChange}
        />);
    });

    return (
        <div className="row clearfix">
            {notes}
        </div>
    )
}

I get Cannot read property 'handleStateChange' of undefined error. What am i doing wrong?

2 Answers 2

2

scope of this inside callback function refer to calling object, not to the react class.So Use ()=> instead of function.

handleStateChange() {
    console.log(this); //just for test
    this.setState({parentState:value})
}

bonuses.forEach((bonus, i) =>{
    let id = 'cell_' + categoryId.toString() + (i + 1).toString();
    notes.push(<NoteCell bonus={bonus}
                         songName={id + '.mp3'}
                         id={id}
                         key={id}
                                    // that is the point
                         handleRowStateChange={this.handleStateChange.bind(this)}
    />);
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot! I suppose this is best practise in all cases?
Yes @Dimitry. You should.
By the way console.log(this) print child element but i need to change parent state. How can i do it?
Check my updated answer and let me know if it works for you.
Sorry, no. this.setState is not a function
|
1

Your this is refering to the bonuses.forEach(function function rather than this from your component class. An arrow function should eliminate that problem.

bonuses.forEach((bonus, i) => {

As an aside, outside of React if you are not using ES6 then you could do this by getting copy of this at the top of your function and then use it inside your function:

render() {
    let notes = [],
        categoryId = this.props.rowNo
        self = this;
        ...

        handleRowStateChange={self.handleStateChange}

But you still have another problem. When you get inside the handleStateChange function, it too will have its own this. You can solve that with a constructor:

class Row extends React.Component {

  constructor (props) {
    super(props);

    this.handleStateChange = this.handleStateChange.bind(this);

  }
...

1 Comment

Thanks for explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.