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;
      }
    
}