0

I'm trying to follow the examples that are coming up here on Stack Overflow and I can't get it to work. It works just fine if I remove everything that says "index" though. It passes in the "page" just fine. Can someone point me in the correct decision please thank you.

My Click function

handleOnClick(page, index) {
    console.log(page);  //page is correct
    console.log(index); //undefined

}

My Render

let self = this;

    playlistList[pos].list.map(function(page, index)

    {if (page.selected) {
      return (
        <tr key={page.id} onClick={() => self.handleOnClick(page, index)}>
          <td>{page.name}</td>
          <td>{page.subComposition.name}</td>
        </tr>
      );}
    }
6
  • Your parameters are passed in backwards Commented Mar 17, 2017 at 20:55
  • @Ju66ernaut which parameters. I reversed the handeClick parameters it doesn't work neither Commented Mar 17, 2017 at 20:56
  • 1
    handleOnClick(index, page) and you pass in page, index Commented Mar 17, 2017 at 20:56
  • @Ju66ernaut no matter how I order the parameters one of them is still undefined. Im just having trouble passing the index Commented Mar 17, 2017 at 20:59
  • 1
    can you reproduce the issue here: jsfiddle.net/zz0o2v0q/2, I tried to repro it based on the code snippets you've provided but I'm not seeing the same behavior Commented Mar 17, 2017 at 21:09

3 Answers 3

2

You're probably better off making a component that handles creating the tr. Then creating the function in that component, then running the function using the props passed down.

Unless you're going to need to change the state, then pass the function down to the component to be used.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to pass the parameters name in the arrow function parentesis, and it's value through the function parentesis.

Like:

onClick={(page, index) => self.handleOnClick(page, index)}

Comments

1

Are you sure that your function handleOnClick it's correct? I think the parameters are wrong.

When you call the function you pass handleOnClick(page, index), and in the method the parameters are handleOnClick(index, page)

1 Comment

they should be the same now. I tried reversing and as well just trying to see what comes out of it but still the index is undefined

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.