0

Here the List get's 'n' values from the app.component. I am trying to write onclick function for each of the buttton. Here, is the code

onChildButtonClick = (val) => {
console.log(val);}



function makeButton(data) {
return (
     <button onClick = {this.onChildButtonClick.bind(null, data.name)} > 
        {data.name} </button>
);}

const List = (props) => {
    const {projectnames} = props
    return (
        <tr>
        <div> {
            projectnames.map(makeButton, this)
        }
        </div>
        </tr>
    )
}

When I try to execute it throws me an error onChildButtonClick is not defined.

1
  • can you share full code of class Commented Sep 20, 2018 at 22:20

1 Answer 1

1

As your code stands above, you're referencing this.onChildButtonClick which is looking within the scope of the function. But your function onChildButtonClick is outside of that scope.

Edit:

I haven't had a chance to test it, but try this out:

function makeButton(data) {

  const onChildButtonClick = (val) => {
    console.log(val);
  }

  return (
       <button onClick = {onChildButtonClick.bind(this, data.name)} >
          {data.name} </button>
  );
}

const List = (props) => {
    const {projectnames} = props
    return (
        <tr>
        <div> {
            projectnames.map(makeButton, this)
        }
        </div>
        </tr>
    )
}
Sign up to request clarification or add additional context in comments.

4 Comments

So what do I do now?
Try removing the "this" from this.onChildButtonClick.
See edit... Didn't have a chance to test, but it might fix the scope issue.
@danielcliff, then consider accepting this answer if it helped you, please.

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.