Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const handleChange = useCallback(({ target }) => {
const { value, checked, type } = target;
const newValue = type === 'checkbox' ? checked : value;
// using dot helps us change nested values
let data;
const isNested = name.includes('.');
if (isNested) {
dot.override = true;
data = dot.str(name, newValue, { ...formData });
}
else data = { ...formData, [name]: newValue };
setValue(newValue);
setFormData(data);
}, [setValue, formData, setFormData, name]);
const oldValue = dot.pick(name, formData) || [];
const { checked } = target;
let newValue;
const index = oldValue.indexOf(value);
if (checked && index < 0) {
newValue = [...oldValue, value]
} else if (!checked && index > -1) {
newValue = oldValue.filter(v => v !== value);
}
// using dot helps us change nested values
let data;
const isNested = name.includes('.');
if (isNested) {
dot.override = true;
data = dot.str(name, newValue, { ...formData });
} else {
data = { ...formData, [name]: newValue };
}
setChecked(checked);
setFormData(data);
}, [value, formData, setFormData, name]);