const [enteredInput, setEnteredInput] = useState("");
const submitHandler = (event) => {
// submit data...
};
const inputChangeHandler = (event) => {
setEnteredInput(event.target.value);
};
<form onSubmit={submitHandler}>
<label htmlFor="name">Your Name</label>
<input type="text" id="name" onChange={inputChangeHandler} />
</form>
const inputRef = useRef();
const submitHandler = (event) => {
submitDataHandler(inputRef.current.value);
};
return (
<form onSubmit={submitHandler}>
<label htmlFor="name">Your Name</label>
<input type="text" id="name" ref={inputRef} />
</form>
);
const nameValidation = (name) => {
const isNameInputValid = name.trim().length !== 0;
return isNameInputValid;
};
const emailValidation = (email) => {
const re = /^(([^<>()\\[\\]\\\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
const [enteredNameInput, setEnteredNameInput] = useState("");
const [isNameInputTouched, setIsNameInputTouched] = useState(false);
// DEFINE Callback function
const inputNameBlurHandler = (event) => {
setIsNameInputTouched(true);
};
// CALLBACK WHEN INPUT LOST FOCUS
<input onBlur={inputBlurHandler} />
// VALIDATE nameInput is INVALID
const nameInputIsInvalid = isNameInputTouched && !isNameInputValid;
// SET INPUT CLASS WITH ERROR STATE
const nameInputClass = nameInputIsInvalid
? "form-control invalid"
: "form-control";
// DISPLAY error message WHEN INPUT IS INVALID
<div className={nameInputClass}>
<label htmlFor="name">Your Name</label>
<input
value={enteredNameInput}
type="text"
id="name"
onBlur={inputNameBlurHandler}
onChange={inputNameChangeHandler}
/>
{nameInputIsInvalid && <p className="error-text">Name must not be empty.</p>}
</div>;
const [enteredEmailInput, setEnteredEmailInput] = useState("");
const [isEmailInputTouched, setIsEmailInputTouched] = useState(false);
const [enteredNameInput, setEnteredNameInput] = useState("");
const [isNameInputTouched, setIsNameInputTouched] = useState(false);
const checkEmail = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;
const isEmailInputValid = checkEmail.test(enteredEmailInput);
const isNameInputValid = enteredNameInput.trim().length !== 0;
const nameInputIsInvalid = isNameInputTouched && !isNameInputValid;
const emailInputIsInvalid = isEmailInputTouched && !isEmailInputValid;
// APPLY FOR MANAGING BUTTON STATUS
const isFormValid = isNameInputValid && isEmailInputValid;
// JSX SUBMIT BUTTON
<div className="form-actions">
<button disabled={!isFormValid}>Submit</button>
</div>;
const submitHandler = (event) => {
event.preventDefault();
// DEFAULT USER HAS TOUCHED INPUT DUE TO CONFIRMATION
setIsFormTouched(true);
// COULD NOT SUBMIT IF FORM IS NOT VALID
if (!isFormValid) {
// DROP OUT DUE TO return
return;
}
fetch(url, { method: "POST", body: ...})
};
// STATE UPDATE
const [enteredInput, setEnteredInput] = useState("");
const [isFormTouched, setIsFormTouched] = useState("");
const submitHandler = (event) => {
event.preventDefault();
...
setEnteredInput("");
setIsFormTouched(false);
};
<input value={enteredInput} />
// useRef UPDATE | NOT RECOMMAND!
const [isFormTouched, setIsFormTouched] = useState("");
const inputRef = useRef();
const submitHandler = (event) => {
event.preventDefault();
inputRef.current.value = "";
setIsFormTouched(false);
};
<input ref={inputRef} />
// in src/hooks/use-input.js
import { useState } from "react";
// MUST start with "use"
const useInput = (validationFunc) => {
const [enteredValue, setEnteredValue] = useState("");
const [isTouched, setIsTouched] = useState(false);
const isValueValid = validationFunc(enteredValue);
const hasError = isTouched && !isValueValid; // false and true
const valueChangeHandler = (event) => {
setEnteredValue(event.target.value);
};
const valueBlurHandler = (event) => {
setIsTouched(true);
};
const resetValueHandler = () => {
setEnteredValue("");
setIsTouched(false);
};
const inputClassHandler = () => {
const className = hasError ? "form-control invalid" : "form-control";
return className;
};
return {
value: enteredValue,
hasError: hasError,
isValueValid: isValueValid,
inputClass: inputClassHandler(),
reset: resetValueHandler,
valueChangeHandler,
valueBlurHandler,
};
};
export default useInput;
import useInput from "../hooks/use-input";
const SimpleForm = (props) => {
const emailValidation = (email) => {
const checkEmailRegExp = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;
const isEmailInputValid = checkEmailRegExp.test(email);
return isEmailInputValid;
};
const nameValidation = (name) => {
const isNameInputValid = name.trim().length !== 0;
return isNameInputValid;
};
const {
value: emailInput,
hasError: emailInputHasError,
isValueValid: isEmailInputValid,
inputClass: emailInputClass,
reset: resetEmailInput,
valueChangeHandler: emailInputChangeHandler,
valueBlurHandler: emailInputBlurHandler,
} = useInput(emailValidation);
const {
value: nameInput,
hasError: nameInputHasError,
isValueValid: isNameInputValid,
inputClass: nameInputClass,
reset: resetNameInput,
valueChangeHandler: nameInputChangeHandler,
valueBlurHandler: nameInputBlurHandler,
} = useInput(nameValidation);
// IF INPUT HAS ERROR, THEN SET BUTTON DISABLE
const isFormValid = !nameInputHasError && !emailInputHasError;
const submitHandler = (event) => {
event.preventDefault();
// CHECK WHETHER EITHER INPUT IS INVALID, THEN DROPOUT
if (!isNameInputValid || !isEmailInputValid) {
return;
}
resetEmailInput();
resetNameInput();
console.log(emailInput);
console.log(nameInput);
};
return (
<form onSubmit={submitHandler}>
<div className={emailInputClass}>
<label htmlFor="email">Your Email</label>
<input
value={emailInput}
type="email"
id="email"
onBlur={emailInputBlurHandler}
onChange={emailInputChangeHandler}
/>
{emailInputHasError && (
<p className="error-text">Email format is not allowed.</p>
)}
</div>
<div className={nameInputClass}>
<label htmlFor="name">Your Name</label>
<input
value={nameInput}
type="text"
id="name"
onBlur={nameInputBlurHandler}
onChange={nameInputChangeHandler}
/>
{nameInputHasError && (
<p className="error-text">Name must not be empty.</p>
)}
</div>
<div className="form-actions">
<button disabled={!isFormValid}>Submit</button>
</div>
</form>
);
};