Anita Anita - 2 months ago 10
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

state
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:
http://jsbin.com/gizamovaco/1/edit?html,js,console,output

Answer

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.

Comments