Anita Anita - 1 year ago 68
React JSX Question

Reactjs onClick: how to set state of clicked buttons in a list

I have a list of buttons and I'm trying to figure out how to get

of each button that was selected like so (so at any one time I know which button(s) has/have been clicked):

// this.state.selectedTransport:
{ car: true, train: false, plane: true } or
{ car: false, train: false, plane: true } or
{ car: true, train: true, plane: true } etc

see code here:,js,console,output

Answer Source

The main idea is to have the handleClick function in a parent component, and then you call it in your child component to update your parent's state and then pass it as props to your different children. I followed your existing structure and it resulted in something like this: JSbin.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download