Framework/React ์™„๋ฒฝ ๊ฐ€์ด๋“œ

[React ์™„๋ฒฝ ๊ฐ€์ด๋“œ] section 8 : ์—ฐ์Šต ํ”„๋กœ์ ํŠธ ์™„๋ฃŒ

yuri lee 2022. 9. 23. 08:13
๋ฐ˜์‘ํ˜•
์ด ๊ธ€์€ udemy์˜ React ์™„๋ฒฝ ๊ฐ€์ด๋“œ with Redux, Next.js, TypeScript ๊ฐ•์ขŒ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

97. ๋ชจ๋“ˆ ์†Œ๊ฐœ

  • ์—ฐ์Šต ํ”„๋กœ์ ํŠธ ์‹œ์ž‘

98. "์‚ฌ์šฉ์ž" ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ

  • Technically, the React import is not required in all React projects as you learned. But you're never wrong with adding it. 
import React from "react";

99.  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ "์นด๋“œ" ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ 

  • ๋‘ ๊ฐœ์˜ css ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• 
import React from "react";
import classes from "./Card.module.css";

const Card = (props) => {
  return (
    <div className={`${classes.card} ${props.className}`}>{props.children}</div>
  );
};

export default Card;

Card.module.css / ๋˜ ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” props๋ฅผ ํ†ตํ•ด ์ž ์žฌ์ ์œผ๋กœ ์™ธ๋ถ€์—์„œ ๋“ค์–ด์˜ค๋Š” ํด๋ž˜์Šค 

 

100.  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ "Button" ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ

  • button component ์ถ”๊ฐ€ 
import React from "react";

import classes from "./Button.module.css";

const Button = (props) => {
  return (
    <button
      className={classes.button}
      type={props.type || "button"}
      onclick={props.onclick}
    >
      {props.children}
    </button>
  );
};

export default Button;

→ props.type์ด ์ •์˜๋˜์ง€ ์•Š์œผ๋ฉด ๋Œ€์•ˆ์œผ๋กœ button ์ด ์‚ฌ์šฉ๋œ๋‹ค. 

→ handler ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด props.onClick๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ๊ผญ ์ด๋ฆ„์„ onClick์œผ๋กœ ํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ, ์ด button์ด ๋‚ด์žฅ๋œ button ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋˜๊ธธ ์›ํ•˜๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ผํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค. 

 

101. ์‚ฌ์šฉ์ž ์ž…๋ ฅ State ๊ด€๋ฆฌํ•˜๊ธฐ 

  • enteredUsername, enteredAge using useState 
  • usernameChangeHandler, ageChangeHandler

102.  ๊ฒ€์ฆ ์ถ”๊ฐ€ ๋ฐ ๋กœ์ง ์žฌ์„ค์ •ํ•˜๊ธฐ

  • ์ž…๋ ฅ๋œ ๋‚˜์ด๊ฐ’์€ ์‚ฌ์‹ค ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌธ์ž์—ด์ด๋‹ค. input์— ์ž…๋ ฅ๋˜๋Š” ์–ด๋–ค ๊ฐ’์ด๋ผ๋„ ํ•ญ์ƒ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค. ์ด๊ฒƒ์ด js์™€ dom์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๊ธฐ์„œ ์ž…๋ ฅ๋˜๋Š” ๋‚˜์ด๊ฐ’์€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด์ด ๋œ๋‹ค. 
if (+enteredAge < 1) {
  return;
}

์ผ๋ฐ˜์ ์€ js ์—์„œ๋Š” '+' ๊ฐ€ ์—†์–ด๋„ ์ž‘๋™ํ•˜์ง€๋งŒ, ์ข€ ๋” ์•ˆ์ „ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด + ๊ธฐํ˜ธ๋ฅผ ๋„ฃ์–ด์„œ ์ž…๋ ฅ๋œ ๋‚˜์ด๊ฐ’์„ ์ˆซ์žํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค! 

103. ์‚ฌ์šฉ์ž ๋ชฉ๋ก ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ

  • userList component ์ถ”๊ฐ€

104. State๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž ๋ชฉ๋ก ๊ด€๋ฆฌํ•˜๊ธฐ

  • App.js ์— userList state๋ฅผ ์ƒ์„ฑ
  const [usersList, setUsersList] = useState([]);

  const addUserHandler = (userName, userAge) => {
    setUsersList((prevUsersList) => {
      return [
        ...prevUsersList,
        { name: userName, age: userAge, id: Math.random().toString() },
      ];
    });
  };

105. "ErrorModal" ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ 

  • ErrorModal component ์ถ”๊ฐ€

106.  ์˜ค๋ฅ˜ State ๊ด€๋ฆฌํ•˜๊ธฐ 

  • error State ์ถ”๊ฐ€ 
const [error, setError] = useState();

  const errorHandler = () => {
    console.log("error!!");
    setError(null);
  };

https://www.udemy.com/course/best-react/

๋ฐ˜์‘ํ˜•