Created
October 2, 2019 12:27
-
-
Save alonbardavid/142ae79b8db39cf886201929a28c2076 to your computer and use it in GitHub Desktop.
Patterns for deriving state gist6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ToppingSelection receives props with the following structure (Typescript notation): | |
// {options:{text:string,value:string},selected:string[],onChange:(value)=>void} | |
class ToppingSelection extends React.Component { | |
handleInputChange = (event)=>{ | |
const value = event.target.name; | |
if ( selected.includes(value)){ | |
onChange(selected.filter(option=>option !== value)); | |
} else { | |
onChange(selected.concat(value)); | |
} | |
} | |
render(){ | |
const {options,selected} = this.props; | |
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