TIL
TIL :: [React ] ๋ก๊ทธ์ธ ๋ฒํผ ํ์ฑํ
sophie0527
2022. 5. 26. 01:19
- useNavigate๋ฅผ ์ฌ์ฉํ์ฌ ๋ค๋น๊ฒ์ดํ (=๋ผ์ฐํ )์ ํ๊ณ ์ ํ ๋!
- ๋ก๊ทธ์ธ ๋ฒํผ ํด๋ฆญ ์ main ์ปดํฌ๋ํธ๋ก ์ด๋
import { Link, useNavigate } from 'react-router-dom';
function LoginSon () {
const navigate = useNavigate();
return(
<div>
<button className="login_button"
onclick={() => {
navigate('/main');
}}
>
๋ก๊ทธ์ธ
</button>
.
.
.
- ๋ก๊ทธ์ธ ๋ฒํผ ์ ํจ์ฑ ๊ฒ์ฆ !
- ๋ก๊ทธ์ธ ๋ฒํผ์ ๋๋ ์ ๋ , id์ password์ ์กฐ๊ฑด์ ๋ง์ถฐ ๋์ด ๊ฐ๊ฒ ํ๋ ๊ฒ๊ณผ ์๋์ด๊ฐ๊ฒ ํ๋ ๊ฒ
import React, { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
function LoginSon () {
const navigate = useNavigate();
const [identify, setIdentify] = useState('');
const [password, setPassword] = useState('');
});
const validation = (idText, pwText) => {
if (!idText.includes('@')) {
return false;
}
if (pwText.length < 7) {
return false;
}
return true;
};
const buttonOnClick = () => {
if (validation(identify, password)) {
alert('๋ก๊ทธ์ธ ๋์์ต๋๋ค.');
navigate('/main-son');
} else {
alert('๋ก๊ทธ์ธ์ ์คํจํ์์ต๋๋ค.');
setIdentify('');
setPassword('');
}
};
return(
<div>
<input
className="login_input"
name="identify"
type="email"
placeholder="์ ํ๋ฒํธ,์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
value={identify}
onChange={(event) => {
setIdentify(event.target.value);
}}
/>
<input
className="login_input"
name="password"
type="password"
minLength={5}
placeholder="๋น๋ฐ๋ฒํธ"
value={password}
onChange={(event) => {
setPassword(event.target.value);
}}
/>
<button className="login_button"
onclick={() => {
navigate('/main');
}}
>
๋ก๊ทธ์ธ
</button>
.
.
.
- ๋ก๊ทธ์ธ ๋ฒํผ์ ํ์ฑํ &๋นํ์ฑํ ์ถ๊ฐํด์ฃผ๊ธฐ
import React, { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
function LoginSon () {
const navigate = useNavigate();
const [identify, setIdentify] = useState('');
const [password, setPassword] = useState('');
const validation = (idText, pwText) => {
if (!idText.includes('@')) {
return false;
}
if (pwText.length < 7) {
return false;
}
const valid = validation(identify, password);
return true;
};
const buttonOnClick = () => {
if (validation(identify, password)) {
alert('๋ก๊ทธ์ธ ๋์์ต๋๋ค.');
navigate('/main-son');
} else {
alert('๋ก๊ทธ์ธ์ ์คํจํ์์ต๋๋ค.');
setIdentify('');
setPassword('');
}
};
return(
<div>
<input
className="login_input"
name="identify"
type="email"
placeholder="์ ํ๋ฒํธ,์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
value={identify}
onChange={(event) => {
setIdentify(event.target.value);
}}
/>
<input
className="login_input"
name="password"
type="password"
minLength={5}
placeholder="๋น๋ฐ๋ฒํธ"
value={password}
onChange={(event) => {
setPassword(event.target.value);
}}
/>
<button
className={valid ? 'login-button active' : 'login-button inactive'}
disabled={!valid}
onClick={buttonOnClick}
>
๋ก๊ทธ์ธ
</button>
.
.
.
.login-button {
border: 0px solid;
border-radius: 3.5px;
color: white;
width: 268.594px;
height: 30px;
align-items: center;
margin: 0;
padding: 0;
position: relative;
&.active {
background-color: #0095f6;
cursor: pointer;
}
&.inactive {
background-color: #c4e1fb;
}
}