Skip to content

Instantly share code, notes, and snippets.

@alonbardavid
Created October 2, 2019 12:32
Show Gist options
  • Save alonbardavid/92fa7ee2a65d6d9000935e1cc251c788 to your computer and use it in GitHub Desktop.
Save alonbardavid/92fa7ee2a65d6d9000935e1cc251c788 to your computer and use it in GitHub Desktop.
Patterns for deriving state gist12
function ToppingSelection(props){
const {options,selected,onChange} = props;
function handleInputChange(event){
const value = event.target.name;
if ( selected.includes(value)){
onChange(selected.filter(option=>option !== value));
} else {
onChange(selected.concat(value));
}
}
return <div>
{options.map(option=>
<label key={option.value}>
{option.text}
<input type={checkbox} checked={selected.includes(option.value)}
onChange=(this.handleInputChange} name={option} />
</label>
)}
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment