22
loading...
This website collects cookies to deliver better user experience
import {
FormControl,
FormLabel,
FormHelperText,
Input,
Button
} from "@chakra-ui/react";
const Form = () => {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
return (
<FormControl id="form" p={50}>
<FormLabel>First and last name</FormLabel>
<Input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<FormLabel>Email address</FormLabel>
<Input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<FormHelperText>We'll never share your email.</FormHelperText>
<FormLabel>Password</FormLabel>
<Input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<Button mt={4} colorScheme="teal" type="submit">
Login
</Button>
</FormControl>
);
};
export default Form;
name
, email
and password
.const initialState = {
name : '',
email : '',
password : ''
}
const Form = () => {
...
}
useReducer
hookimport { useState, useReducer } from "react";
reducer.js
where it will have a function that handles our state.// reducer.js
function reducer (state, action) {
}
state
this will be the state we receive from the dispatch method that was executed in our case from App.js
action
someone calls it the actionObject
because when dispatched it looks like this:
const action = {
type: 'FETCH'
data : "Alejo"
};
switch
casesfunction reducer (state, action) {
switch(action.type){
case "UPDATE" :
return {
...state,
[action.key] : action.value
}
default:
return state
}
}
action.type
to the changes the reducer is going to implement. In other words, the switch will determine those changes like modifying the state BASED on the value of the action.type
...state
and what it does is basically copying the entire state, in order to only modify only the [action.key] : action.value
App.js
componentimport reducer from "./reducer"
import { useState, useReducer } from "react";
const initialState = {
name: "",
email: "",
password: ""
};
const Form = () => {
// const [name, setName] = useState("");
// const [email, setEmail] = useState("");
// const [password, setPassword] = useState("");
const [reducerState, dispatch] = useReducer(reducer, initialState);
...
}
useState
, to the initialState
and change the onChange
setting state functions to the reducer ones using dispatch.<Input
type="text"
value={reducerState.name}
onChange={(e) =>
dispatch({
type: "UPDATE",
value: e.target.value,
key: "name"
})
}
/>
<Input
type="email"
value={reducerState.email}
onChange={(e) =>
dispatch({
type: "UPDATE",
value: e.target.value,
key: "email"
})
}
/>
<Input
type="password"
value={reducerState.password}
onChange={(e) =>
dispatch({
type: "UPDATE",
value: e.target.value,
key: "password"
})
}
/>
console.log
inside a submit()
function, so we get to see the complete state from the reducer, and confirm that we have refactored the form successfully:const Form = () => {
function submit() {
console.log({
name: reducerState.name,
email: reducerState.email,
password: reducerState.password
});
}
return (
...
<Button onClick={() => submit()} mt={4} colorScheme="teal" type="submit">
Login
</Button>
);
};
export default Form;
{name: 'asadsasd', email: '[email protected]', password: 'sadadada'}
useReducer
// App.js
import {
FormControl,
FormLabel,
FormHelperText,
Input,
Button
} from "@chakra-ui/react";
import reducer from "./reducer";
import { useState, useReducer } from "react";
const initialState = {
name: "",
email: "",
password: ""
};
const Form = () => {
const [reducerState, dispatch] = useReducer(reducer, initialState);
function submit() {
console.log({
name: reducerState.name,
email: reducerState.email,
password: reducerState.password
});
}
return (
<FormControl id="form" p={50}>
<FormLabel>First and last name</FormLabel>
<Input
type="text"
value={reducerState.name}
onChange={(e) =>
dispatch({
type: "UPDATE",
value: e.target.value,
key: "name"
})
}
/>
<FormLabel>Email address</FormLabel>
<Input
type="email"
value={reducerState.email}
onChange={(e) =>
dispatch({
type: "UPDATE",
value: e.target.value,
key: "email"
})
}
/>
<FormHelperText>We'll never share your email.</FormHelperText>
<FormLabel>Password</FormLabel>
<Input
type="password"
value={reducerState.password}
onChange={(e) =>
dispatch({
type: "UPDATE",
value: e.target.value,
key: "password"
})
}
/>
<Button onClick={() => submit()} mt={4} colorScheme="teal" type="submit">
Login
</Button>
</FormControl>
);
};
export default Form;
// reducer.js
function reducer (state, action) {
switch(action.type){
case "UPDATE" :
return {
...state,
[action.key] : action.value
}
default:
return state
}
}
export default reducer