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 [prevState, setState] = React.useState([]);
2
3setState(prevState => [...prevState, 'somedata'] );