1- Through Input
2
3 const [state, setState] = useState({ fName: "", lName: "" });
4 const handleChange = e => {
5 const { name, value } = e.target;
6 setState(prevState => ({
7 ...prevState,
8 [name]: value
9 }));
10 };
11
12 <input
13 value={state.fName}
14 type="text"
15 onChange={handleChange}
16 name="fName"
17 />
18 <input
19 value={state.lName}
20 type="text"
21 onChange={handleChange}
22 name="lName"
23 />
24 ***************************
25
26 - Through onSubmit or button click
27
28 setState(prevState => ({
29 ...prevState,
30 fName: 'your updated value here'
31 }));
32
1const [currentApp, setCurrentApp] = useState({
2 userID: null,
3 hospitalName: null,
4 hospitalID: null,
5 date: null,
6 time: null,
7 timestamp: null,
8 slots: 1,
9 appointmentType: null
10 })
11
12const [appointmentType, setAppointmentType] = useState({
13 Granulocytes: {
14 bloodType:null,
15 message: null
16 }
17})
18
19const handleGranChange = (e) => {
20 setAppointmentType({...appointmentType, Granulocytes : {
21 ...appointmentType.Granulocytes,
22 [e.target.id] : e.target.value}
23 })
24 setCurrentApp({ ...currentApp, ['appointmentType'] : appointmentType })
25 console.log(currentApp)
26}