0

I'm creating buttons using map to loop over an array. Each button is an item in the array. I'm also using React.createElement to create each button.

['NICK', 'NKJR', 'NKTNS'].map(function (brand) {
  return React.createElement('button', {
    onClick: (e) => { console.log(e.target) }
  }, brand)
})

..however, for the onClick listener, how do I pass in the 'value' for each button?

For example, I'm trying to get the console.log to log 'NICK', 'NKJR' or 'NKTNS' depending on which button is clicked. Right now when I click a button it just logs <button>NICK</button> for example (I want 'NICK')

1
  • 1
    brand is already in scope of your click handler definition so why not just directly use it? Otherwise, you could use e.target.textContent or e.target.value though YMMV on that last one if you don't also directly assign value: brand Commented Aug 26, 2021 at 0:29

1 Answer 1

1

I believe you should have access to the brand value by doing this:

onClick: () => {console.log(brand)}}

Since the value of brand is accessing within the .map() you are doing

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

Comments

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.