React์ ์์ ๐ง ๊ฐ์ฅ ๋จ์ํจ
Form validation can be a nightmare sometimes. After trying out some patterns of code, I came to a conclusion. That this can be an optimal solution without any plugins.
NOTE: Better ways are always welcome & I'm in search for it โ
Code Sandbox Link
์ ๋ ฅ ํ๋
Let's get started. First things first. Let's create a component for input field.
- input field to accept user inputs and pass other common props
- handleInvalid method to do it's magic when it detects error
- onChange method to reset the error and pass on the control to the parent component
import React from "react";
/** We'll be using this only for username and password **/
export const InputField = props => {
const { message, ...rest } = props; // Filter what you need and transfer rest to input
let [error, setError] = React.useState(false); //display error message
const handleInvalid = e => {
e.preventDefault();
setError(true);
props.handleInvalid(e);
}; // Handle error
const onChange = e => {
setError(false);
props.handleChange(e);
}; // Reset error
// The Component
return (
<div className="input-container">
<input {...rest} onInvalid={handleInvalid} onChange={onChange} />
{error ? <span className="error-message">{message}</span> : ""}
</div>
);
};
์์ ๊ตฌ์ฑ์์
Now let's create a Form Component that will wrap our input fields around a form and some methods to handle it.
- We have a state variable which will hold our form object
- handleChange method to handle specific updates in input fields according to name.
- handleSubmit method which would help us hitting API once all fields are filled properly
- action="javascript:void(0)" this was of my particular interest when I was trying to find out the best way to handle forms. It would prevent the form element from doing default work.
import React from "react";
import { InputField } from "./InputField";
export const Form = props => {
const [state, setState] = React.useState({ mail: "", password: "" });
const handleChange = e =>
setState({ ...state, [e.target.name]: e.target.value });
const handleSubmit = e => console.info("FORMDATA", state);
return (
<form
action="javascript:void(0)"
onChange={handleChange}
onSubmit={handleSubmit}
>
<InputField name="mail" type="email" message="We need a proper mail id" />
<InputField name="password" minLength="9" type="password" message="Not a valid password"
/>
<input type="submit" value="Login" />
</form>
);
};
App.css
์ด์ css๋ฅผ ์ถ๊ฐํ์ฌ ์ ์ด๋ ์กฐ๊ธ ์ด์ํ๊ฒ ๋ณด์ด๋๋ก ํฉ์๋ค.
.error-message {
color: darkred;
text-align: right;
font-size: 12px;
}
.input-container {
display: flex;
flex-direction: column;
width: 40%;
margin: auto;
}
.input-container > input {
border-top: none;
border-right: none;
border-left: none;
margin-bottom: 5px;
margin-top: 20px;
}
input:invalid {
border-color: darkred;
}
๋ฐ๋ผ์ React์์ ์์์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์ผ๋ง๋ ์ฌ์ธ ์ ์๋์ง ๊ธฐ๋ํฉ๋๋ค. ๋ชจ๋ ํจํด์ ๋๋ค. ์์์ ๋ํ ์ถ๊ฐ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์ฐพ๊ณ ์์ผ๋ฏ๋ก ๋นํ๊ฐ๋ฅผ ๊ฐ์ฅ ํ์ํฉ๋๋ค.
์ด๊ฒ๋ณด๋ค ๋ ์ํ ์ ์๋ ์ผ์ด ์๋ค๋ฉด ์๋ ค์ฃผ์ธ์.
Reference
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(React์ ์์ ๐ง ๊ฐ์ฅ ๋จ์ํจ), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://dev.to/droidmakk/forms-in-react-the-simplest-39peํ ์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค