Skip to content

Instantly share code, notes, and snippets.

@eugenehp
Created April 6, 2021 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugenehp/512890ebac2445f8e16bc0be812fac53 to your computer and use it in GitHub Desktop.
Save eugenehp/512890ebac2445f8e16bc0be812fac53 to your computer and use it in GitHub Desktop.
reddit question
// https://www.reddit.com/r/react/comments/mkuhg4/how_do_i_handle_this_i_want_to_set_state_on_lots/
const data = {
'loc': 'location',
'svc': 'serviceType',
'decorator': 'decorator',
...
};
const handleChange = (e) => {
const {name, checked} = e.target;
const value = data[name]; // 'loc' gets converted to 'location'
if(value){
let newState = {}
newState[value] = checked; // sets to {location: checked}
this.setState(newState)
}else{
// handle error here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment