update object in react hooks
- Through Input
const [state, setState] = useState({ fName: "", lName: "" });
const handleChange = e => {
const { name, value } = e.target;
setState(prevState => ({
...prevState,
[name]: value
}));
};
<input
value={state.fName}
type="text"
onChange={handleChange}
name="fName"
/>
<input
value={state.lName}
type="text"
onChange={handleChange}
name="lName"
/>
***************************
- Through onSubmit or button click
setState(prevState => ({
...prevState,
fName: 'your updated value here'
}));
const [currentApp, setCurrentApp] = useState({
userID: null,
hospitalName: null,
hospitalID: null,
date: null,
time: null,
timestamp: null,
slots: 1,
appointmentType: null
})
const [appointmentType, setAppointmentType] = useState({
Granulocytes: {
bloodType:null,
message: null
}
})
const handleGranChange = (e) => {
setAppointmentType({...appointmentType, Granulocytes : {
...appointmentType.Granulocytes,
[e.target.id] : e.target.value}
})
setCurrentApp({ ...currentApp, ['appointmentType'] : appointmentType })
console.log(currentApp)
}
Also in JavaScript:
- the update operation document must contain atomic operators mongodb
- threejs
- navigation.openDrawer is not a function react native
- usestate() react
- jquery on dom change
- how to smooth scroll in javascript
- jquery add style
- javascript muuttujan määrittely
- mongoos populate a ref
- $.extend jquery
- javascript on keypu
- javascript time execution
- count word and space in text javascript
- javascript bind multiple arguments
- how to check if enter is pressed javascript
- pass header in ajax
- javascript match image attribute value
- reactjs start
- ajax file upload jquery
- event listener javascript
- variables in js class
- SyntaxError: Unexpected end of JSON input while parsing near '...version":"2.0.6","devD' cli
- making promises in js
- express.json vs bodyparser.json