name
μ Inputμ λν΄μ use-input
컀μ€ν
ν
μ μ΄μ©ν κ²μ²λΌ, λμΌνκ² λ³΅μ¬ν΄μ ν΄λΉ useInput
컀μ€ν
ν
μ κ°μ Έμ€κ³ , name
μ μ μ©ν κ²μ²λΌ 컀μ€ν
ν
μμ λ°νν κ°λ€μ μ΄λ¦μ email
μ μ©μΌλ‘ μμ ν΄μ€λ€.// name
const {
value: enteredName,
isValid: enteredNameIsValid,
hasError: nameInputHasError,
valueChangeHandler: nameChangeHandler,
inputBlurHandler: nameBlurHandler,
reset: resetNameInput,
} = useInput((value) => value.trim() !== "");
// e-mail
const {
value: enteredEmail,
isValid: enteredEmailIsValid,
hasError: emailInputHasError,
valueChangeHandler: eamilChangeHandler,
inputBlurHandler: eamilBlurHandler,
reset: resetEmailInput,
} = useInput((value) => value.trim() !== "" && value.includes("@"));
useState
λ‘ μμ±νλ κ°κ³Ό ν΄λΉ μ»΄ν¬λνΈ λ΄μμλ§ μ¬μ©νλ μ μ© ν¨μλ€μ λͺ¨λ μ§μμ€λ€.// const [enteredEmail, setEnteredEmail] = useState("");
// const [enteredEmailTouched, setEnteredEmailTouched] = useState(false);
// const enteredEmailIsValid =
// enteredEmail.trim() !== "" && enteredEmail.includes("@");
// const emailInputIsInvalid = !enteredEmailIsValid && enteredEmailTouched;
formSubmitssionHandler
ν¨μμλ useStateλ₯Ό μ¬μ©ν΄μ 리μ
ν΄μ£Όμλ λΆλΆμ μ§μ°κ³ , 컀μ€ν
ν
μ 리μ
ν¨μμΈ resetEmailInput
(reset
)μ νΈμΆν΄ μ€λ€.const formSubmitssionHandler = (event) => {
event.preventDefault();
if (!enteredNameIsValid || !enteredEmailIsValid) {
return;
}
resetNameInput();
// setEnteredEmail("");
// setEnteredEmailTouched(false);
resetEmailInput();
};
useInput
컀μ€ν
ν
μμ μ¬μ©νλ hasError
λ³μλ₯Ό κΈ°λ°μΌλ‘ μΌνμ°μ°μλ₯Ό μ¬μ©ν class μ€μ λν 컀μ€ν
ν
μμ κ°μ Έμ¨ emailInputHasError
μΌλ‘ μμ ν΄μ€λ€.const emailInputClasses = emailInputHasError // true μ΄λ©΄,
? "form-control invalid" // κ²½κ³ css
: "form-control";
email
μ μ
λ ₯νλ input νκ·Έμ ν¬μΈν° ν΄μ£Όμλ ν¨μλ useInput
컀μ€ν
ν
μ ν¨μλ€λ‘ λ체ν΄μ€λ€. μλμ μΌνμ°μ°μλ₯Ό μ΄μ©ν λΆλΆλ emailInputHasError
λ‘ μμ ν΄μ€λ€.<input
type="email"
id="email"
onChange={eamilChangeHandler}
onBlur={eamilBlurHandler}
value={enteredEmail}
/>;
{
emailInputHasError && <p className="error-text">Email must not be empty.</p>;
}
SimpleInput
μ»΄ν¬λνΈ λμ , BasicForm
μ»΄ν¬λνΈλ₯Ό μ΄μ©ν΄μ μ΄μ μ²λΌ 컀μ€ν
ν
μ μ΄μ©ν μ ν¨μ± κ²μ¦μ λμ ν΄λ³Ό κ²μ΄λ€.// import SimpleInput from "./components/SimpleInput";
import BasicForm from "./components/BasicForm";
function App() {
return (
<div className="app">
{/* <SimpleInput /> */}
<BasicForm />
</div>
);
}
export default App;
App
μ»΄ν¬λνΈ λ΄λΆμ SimpleInput
μ»΄ν¬λνΈ λμ BasicForm
μ»΄ν¬λνΈλ‘ λ체νλ€.before
const BasicForm = (props) => {
return (
<form>
<div className="control-group">
<div className="form-control">
<label htmlFor="name">First Name</label>
<input type="text" id="name" />
</div>
<div className="form-control">
<label htmlFor="name">Last Name</label>
<input type="text" id="name" />
</div>
</div>
<div className="form-control">
<label htmlFor="name">E-Mail Address</label>
<input type="text" id="name" />
</div>
<div className="form-actions">
<button>Submit</button>
</div>
</form>
);
};
export default BasicForm;
after
import useInput from "../hooks/use-input";
const BasicForm = (props) => {
// First Name
const {
value: enteredFirstName,
isValid: enteredFirstNameIsValid,
hasError: firstNameInputHasError,
valueChangeHandler: firstNameChangeHandler,
inputBlurHandler: firstNameInputBlurHandler,
reset: resetFirstName,
} = useInput((value) => value.trim() !== "");
// Last Name
const {
value: enteredLastName,
isValid: enteredLastNameIsValid,
hasError: lastNameInputHasError,
valueChangeHandler: lastNameChangeHandler,
inputBlurHandler: lastNameInputBlurHandler,
reset: resetLastName,
} = useInput((value) => value.trim() !== "");
// email
const {
value: enteredEmail,
isValid: enteredEmailIsValid,
hasError: emailInputHasError,
valueChangeHandler: emailChangeHandler,
inputBlurHandler: emailInputBlurHandler,
reset: resetEmail,
} = useInput((value) => value.trim() !== "" && value.includes("@"));
let formIsValid = false;
if (
enteredFirstNameIsValid &&
enteredLastNameIsValid &&
enteredEmailIsValid
) {
formIsValid = true;
} else {
formIsValid = false;
}
const formSubmitssionHandler = (e) => {
e.preventDefault();
if (
!enteredFirstNameIsValid ||
!enteredLastNameIsValid ||
!enteredEmailIsValid
) {
return;
}
resetFirstName();
resetLastName();
resetEmail();
};
const firstNameInputClasses = firstNameInputHasError
? "form-control invalid"
: "form-control";
const lastNameInputClasses = lastNameInputHasError
? "form-control invalid"
: "form-control";
const emailInputClasses = emailInputHasError
? "form-control invalid"
: "form-control";
return (
<form onSubmit={formSubmitssionHandler}>
<div className="control-group">
<div className={firstNameInputClasses}>
<label htmlFor="name">First Name</label>
<input
type="text"
id="name"
value={enteredFirstName}
onChange={firstNameChangeHandler}
onBlur={firstNameInputBlurHandler}
/>
{firstNameInputHasError ? (
<p className="error-text">First Name μ΄ λΉμ΄μμ΅λλ€.</p>
) : (
""
)}
</div>
<div className={lastNameInputClasses}>
<label htmlFor="name">Last Name</label>
<input
type="text"
id="name"
value={enteredLastName}
onChange={lastNameChangeHandler}
onBlur={lastNameInputBlurHandler}
/>
{lastNameInputHasError ? (
<p className="error-text">Last Name μ΄ λΉμ΄μμ΅λλ€.</p>
) : (
""
)}
</div>
</div>
<div className={emailInputClasses}>
<label htmlFor="name">E-Mail Address</label>
<input
type="text"
id="email"
value={enteredEmail}
onChange={emailChangeHandler}
onBlur={emailInputBlurHandler}
/>
{emailInputHasError ? (
<p className="error-text">E-Mail μ£Όμκ° λ€λ¦
λλ€.</p>
) : (
""
)}
</div>
<div className="form-actions">
<button disabled={!formIsValid}>Submit</button>
</div>
</form>
);
};
export default BasicForm;
useInput
μ μ λ¬νλ ν¨μ λ‘μ§ λ°λ‘ μ μ λ³μλ‘ λΉΌκΈ°.const isNotEmpty = (value) => value.trim() !== "";
const isEmail = (value) => value.includes("@") && isNotEmpty;
// Name
const {
value: enteredLastName,
isValid: enteredLastNameIsValid,
hasError: lastNameInputHasError,
valueChangeHandler: lastNameChangeHandler,
inputBlurHandler: lastNameInputBlurHandler,
reset: resetLastName,
} = useInput(isNotEmpty);
// email
const {
value: enteredEmail,
isValid: enteredEmailIsValid,
hasError: emailInputHasError,
valueChangeHandler: emailChangeHandler,
inputBlurHandler: emailInputBlurHandler,
reset: resetEmail,
} = useInput(isEmail);
&&
λ‘ λ‘μ§ κ°κ²°νκ² μ€μ΄κΈ°// {
// firstNameInputHasError ? (
// <p className="error-text">First Name μ΄ λΉμ΄μμ΅λλ€.</p>
// ) : (
// ""
// );
// }
{
firstNameInputHasError && (
<p className="error-text">First Name μ΄ λΉμ΄μμ΅λλ€.</p>
);
}
formIsValid
κ° νλλ‘ κ²μ¬ν μ μλ€. formIsValid
λ₯Ό μ΄μ©ν΄μ λ²νΌμ λΉνμ±ν νκΈ° λλ¬Έμ λ²νΌμ λΉνμ±νν μνμμλ μ μ΄μ νΌμ μ μΆν μ μκΈ° λλ¬Έμ΄λ€.let formIsValid = false;
if (
enteredFirstNameIsValid &&
enteredLastNameIsValid &&
enteredEmailIsValid
) {
formIsValid = true;
} else {
formIsValid = false;
}
...
const formSubmitssionHandler = (e) => {
e.preventDefault();
if (
// !enteredFirstNameIsValid ||
// !enteredLastNameIsValid ||
// !enteredEmailIsValid
!formIsValid
) {
return;
}
// first name reset
resetFirstName();
// last name reset
resetLastName();
// email reset
resetEmail();
};
SimpleInput
κ³Ό λμΌν λ°©μμΌλ‘ μλνλ κ±Έ μ μ μλ€.π¨ ν΄λΉ ν¬μ€ν μ Udemyμ βReact μλ²½ κ°μ΄λβ κ°μλ₯Ό λ² μ΄μ€λ‘ ν κΈ°λ‘μ λλ€.
βπ» κ°μ git repo λ°λ‘κ°κΈ°