ref

quokkaยท2022๋…„ 1์›” 28์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
9/12
post-thumbnail

๐Ÿšจย  ์˜ค๋Š˜์€ ref์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ธฐ,,,,

ref ๊ฐ€ ํ•˜๋Š” ์ผ์€ ??

=ย  ref๋Š” ์ƒ๋‹นํžˆ ๊ฐ•๋ ฅํ•œ ์•„์ด๋‹ค.

๐Ÿ‘‰ย  ๊ธฐ๋ณธ ์ƒํƒœ์—์„œ์˜ ref๋Š” ๋‹ค๋ฅธ DOM์š”์†Œ๋กœ ์—‘์„ธ์Šคํ•ด์„œ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์—ญํ• ์ด๋‹ค.

๐ŸŒ€ย ref๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ,

์˜ˆ์‹œ)

import React, { useState } from "react";

import Card from "../UI/Card";
import Button from "../UI/Button";
import ErrorModal from "../UI/ErrorModal";
import Wrapper from "../Helpers/Wrapper";
import classes from "./AddUser.module.css";

const AddUser = (props) => {
  const [enteredUsername, setEnteredUsername] = useState("");
  const [enteredAge, setEnteredAge] = useState("");
  const [error, setError] = useState();

  const addUserHandler = (event) => {
    event.preventDefault();
    if (enteredUsername.trim().length === 0 || enteredAge.trim().length === 0) {
      setError({
        title: "Invalid input",
        message: "Please enter a valid name and age (non-empty values).",
      });
      return;
    }
    if (+enteredAge < 1) {
      setError({
        title: "Invalid age",
        message: "Please enter a valid age (> 0).",
      });
      return;
    }
    props.onAddUser(enteredUsername, enteredAge);
    setEnteredUsername("");
    setEnteredAge("");
  };

  const usernameChangeHandler = (event) => {
    setEnteredUsername(event.target.value);
  };

  const ageChangeHandler = (event) => {
    setEnteredAge(event.target.value);
  };

  const errorHandler = () => {
    setError(null);
  };

  return (
    <Wrapper>
      {error && (
        <ErrorModal
          title={error.title}
          message={error.message}
          onConfirm={errorHandler}
        />
      )}
      <Card className={classes.input}>
        <form onSubmit={addUserHandler}>
          <label htmlFor="username">Username</label>
          <input
            id="username"
            type="text"
            value={enteredUsername}
            onChange={usernameChangeHandler}
          />
          <label htmlFor="age">Age (Years)</label>
          <input
            id="age"
            type="number"
            value={enteredAge}
            onChange={ageChangeHandler}
          />
          <Button type="submit">Add User</Button>
        </form>
      </Card>
    </Wrapper>
  );
};

export default AddUser;
					<input
            id="age"
            type="number"
            value={enteredAge}
            onChange={ageChangeHandler}
          />
  • ์—ฌ๊ธฐ ๋ณด์ด๋Š” ์ฝ”๋“œ๋“ค ์ค‘์—์„œ ์œ„์™€ ๊ฐ™์ด input ํƒœ๊ทธ์—์„œ ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๋ฐ”๋ฅผ ์ถ”์ ํ•ด์„œ ๊ด€๋ฆฌํ•œ๋‹ค.
const AddUser = (props) => 
  • state์—์„œ ์ƒํƒœ๊ฐ€ ์กด์žฌํ•˜๋ฉฐ ๋งค ํ‚ค ์ž…๋ ฅ์— ๋”ฐ๋ผ ์ƒํƒœ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋œ๋‹ค.
{
  const [enteredUsername, setEnteredUsername] = useState("");
  const [enteredAge, setEnteredAge] = useState("");
  const [error, setError] = useState();
  • ์œ ์ €์˜ ํ‚ค ์ž…๋ ฅ์„ ๋”ฐ๋ผ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ์ด๋ฅผ ์ƒํƒœ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค!.
				<input
            id="age"
            type="number"
            value={enteredAge}
            onChange={ageChangeHandler}
          />
  • ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ €์žฅํ•œ ์ƒํƒœ๋ฅผ input ์œผ๋กœ ํ”ผ๋“œ๋ฐฑํ•˜๊ณ  ๋‚˜์ค‘์— ๊ทธ ์ƒํƒœ๋ฅผ ํ™œ์šฉํ•ด์„œ
props.onAddUser(enteredUsername, enteredAge);
    setEnteredUsername("");
    setEnteredAge("");
  };
  • input์„ ๋ฆฌ์…‹ํ•˜๋Š” ํ•œํŽธ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ณณ์œผ๋กœ ๋ณด๋‚ด์ค€๋‹ค.

๐Ÿ‘‰ย  ref๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์— form๋งŒ ์ œ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋Š”๋ฐ ํ‚ค ์ž…๋ ฅ๋งˆ๋‹ค ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฑด ์ข€ ... ๋ถˆํŽธ์Šค ํ•  ์ˆ˜์žˆ๋‹ค... (๊ณผํ•˜๋‹ค..) ์ด๋Ÿด ๋•Œ ํ•„์š”ํ•œ๊ฒƒ์€ ?!!! ref!!!

๐ŸŒ€ย ref๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

ref๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์—ฐ๊ฒฐ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘‰ย  ๋ Œ๋”๋ง ๋  HTML์š”์†Œ์™€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๊ฐ„์˜ ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅ!!

๐Ÿ‘‰ย  useRef๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์ด ์ค‘์š”!! ํ•˜๋‹ค. ๋‚˜์ค‘์— ํ™œ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์—ฐ๊ฒฐํ•˜๋ ค๋Š” ์š”์†Œ์— ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

์‚ฌ์šฉ๋ฐฉ๋ฒ•

๋จผ์ €, ref๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด

import React, { useState, useRef } from "react";
  • ref๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•œ๋‹ค.
const AddUser = (props) => {
  const nameInputRef = useRef();
  const ageInputRef = useRef();

  const [enteredUsername, setEnteredUsername] = useState("");
  const [enteredAge, setEnteredAge] = useState("");
  const [error, setError] = useState();
  • ์ฝ”๋“œ ์•ˆ์—, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•œ๋‹ค.
  • ์˜ค์ง ํ•จ์ˆ˜ํ˜•์—์„œ๋งŒ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ref๋ฅผ ์—ฐ๊ฒฐํ•˜๋ ค๋Š” HTML์ฝ”๋“œ๋กœ ๊ฐ€์„œ ํŠน๋ณ„ํ•œ prop์„ ์ถ”๊ฐ€ํ•œ๋‹ค. โ‡’ ref prop
					<input
            id="age"
            type="number"
            value={enteredAge}
            onChange={ageChangeHandler}
						ref={ageInputRef}
          />
  • key prop๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‚ด์žฅ prop์ด๋ฉฐ ์–ด๋Š HTML์š”์†Œ์—๋„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. โ‡’ ์–ด๋–ค HTML์š”์†Œ๋“  ํ•˜๋‚˜์˜ ๋ ˆํผ๋Ÿฐ์Šค์™€ ์—ฐ๊ฒฐ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๋งค์ผ๋งค์ผ์ด ๊ธฐ๋Œ€๋˜๋Š” ๊ฐœ๋ฐœ์ž ^^

0๊ฐœ์˜ ๋Œ“๊ธ€