Last active
June 30, 2018 11:19
-
-
Save sagar-gavhane/91cc97637e399c57ecc34b6bd6ac7536 to your computer and use it in GitHub Desktop.
HandleInputChange for React function
This file contains 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
handleInputChange(name, value, args = {}) { | |
const { key, ...restArgs } = args; | |
let obj = {}; | |
if (typeof key !== 'undefined') { | |
if (Object.keys(restArgs).length === 0 && restArgs.constructor === Object) { | |
obj = { ...this.state[key], ...{ [name]: value } }; | |
} else { | |
obj = { ...this.state[key], ...{ [name]: value, ...restArgs } }; | |
} | |
this.setState((prevState) => { | |
return { | |
[key]: obj, | |
}; | |
}); | |
} else { | |
this.setState((prevState) => { | |
return { | |
[name]: value, | |
}; | |
}); | |
} | |
} |
Author
sagar-gavhane
commented
Jun 30, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment