Skip to content

Instantly share code, notes, and snippets.

@sagar-gavhane
Last active June 30, 2018 11:19
Show Gist options
  • Save sagar-gavhane/91cc97637e399c57ecc34b6bd6ac7536 to your computer and use it in GitHub Desktop.
Save sagar-gavhane/91cc97637e399c57ecc34b6bd6ac7536 to your computer and use it in GitHub Desktop.
HandleInputChange for React function
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,
};
});
}
}
@sagar-gavhane
Copy link
Author

handleInputChange(name, value){
        this.setState((prevState) => {
            return {
                [name]: value
            };
        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment