简体   繁体   中英

ReactJS: get button 'value' on click

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')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM