-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm-reducer.jsx
More file actions
58 lines (48 loc) · 1.84 KB
/
Copy pathForm-reducer.jsx
File metadata and controls
58 lines (48 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { useReducer } from "react";
const Home = () => {
const initialValue = {
firstName: "",
lastName:"",
designation:"",
email:"",
mobile:""
}
const handleSubmit = (e) => {
e.preventDefault();
console.log(`Final api payload`, state)
}
const [state, dispatch] = useReducer(formControl, initialValue);
function formControl (state, action) {
switch(action.type){
case 'firstname':{
return {...state, firstName: action.payload}
}
case 'lastname':{
return {...state, lastName: action.payload}
}
case 'designation':{
return {...state, designation: action.payload}
}
case 'email':{
return {...state, email: action.payload}
}
case 'mobile':{
return {...state, mobile: action.payload}
}
default : return state;
}
}
return (
<>
<form onSubmit={handleSubmit}>
<input type="text" placeholder="Enter First Name" onChange={(e) => dispatch({type:'firstname', payload:e.target.value})}/>
<input type="text" placeholder="Enter Last Name" onChange={(e) => dispatch({type:'lastname', payload:e.target.value})}/>
<input type="text" placeholder="Enter Designation" onChange={(e) => dispatch({type:'designation', payload:e.target.value})}/>
<input type="email" placeholder="email" onChange={(e) => dispatch({type:'email', payload:e.target.value})}/>
<input type="number" placeholder="Mobile" onChange={(e) => dispatch({type:'mobile', payload:e.target.value})}/>
<button type="submit">Submit</button>
</form>
</>
)
}
export default Home;