header-img
Info :
728x90

보톡 useRef κΉŒμ§€λŠ” ν”νžˆλ“€ μ•ˆλ‹€κ³  μƒκ°ν•˜λŠ”λ°,

λ§Œμ•½ λ‚΄κ°€ μ–΄λ–€ κ³³μ—μ„œ μ»΄ν¬λ„ŒνŠΈλ“€μ„ μΌκ΄„λ‘œ μ •μ˜ν•΄λ‘κ³ , λΆ€λͺ¨ λ‹¨μ—μ„œ κΊΌλ‚΄μ–΄μ„œ μ‚¬μš©ν•˜λŠ” ν˜•νƒœμ˜ λ‘œμ§μ„ μ§ λ‹€λ©΄ μ–΄λ–»κ²Œ 될까? μ΄λ•Œλ„ refλ₯Ό λ‹€λ£¨λŠ” caseκ°€ μ‘΄μž¬ν•œλ‹€λ©΄?

​

정닡은 ForwardRef 이닀. λΆ€λͺ¨μ—μ„œ μžμ‹μ˜ ref μš”μ†Œλ“€μ„ 닀루고 싢을 λ•Œ μ‚¬μš©ν•˜λŠ” 문법.

​

또,

μžμ‹λ‹¨μ— μžˆλŠ” function 을 λΆ€λͺ¨μ—μ„œ μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄?

(보톡은 λΆ€λͺ¨μ—μ„œ μ‚¬μš©ν•˜λŠ” 것이 λ§žμ§€λ§Œ, λ¬΄μž‘μ • κ·Έλ ‡κ²Œ λ‘œμ§μ„ μ§œλ²„λ¦¬λ©΄ ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈ 듀이 λ§Žμ•„μ§ˆμˆ˜λ‘ μ΅œμƒμœ„, μƒμœ„ 둜직이 λΉ„λŒ€ν•΄μ§μ„ 막을 수 μ—†κ²Œ λœλ‹€.)

​

정닡은 useImperativeHandle 이닀. μžμ‹μ˜ λ¬Έλ²•μ΄λ‚˜ λ³€μˆ˜λ₯Ό λΆ€λͺ¨ λ‹¨μ—μ„œ μ‚¬μš©ν•  수 있게 ν•΄μ£ΌλŠ” 문법.

​

μš°μ„ .. forwardRef λΆ€ν„°...

​

forwardRef 없이 μžμ‹μ˜ ref λ₯Ό 뢈러였렀고 μ‹œλ„λ₯Ό ν•΄λ³΄μž.

import React, { useRef } from "react";

function Input({ ref }) {
  return <input type="text" ref={ref} />;
}

function Field() {
  const inputRef = useRef(null);

  function handleFocus() {
    inputRef.current.focus();
  }

  return (
    <>
      <Input ref={inputRef} />
      <button onClick={handleFocus}>μž…λ ₯λž€ 포컀슀</button>
    </>
  );
}

 

μš°λ¦¬κ°€ μ‚¬μš©ν•˜λŠ” ν”ν•œ ref 문법이닀.

μ΄λ•Œ μ•„λ§ˆ μ›ν•˜λŠ” λŒ€λ‘œ 정상 μž‘λ™μ΄ λ˜μ§€ μ•Šκ³ , μ•„λž˜μ™€ 같은 κ²½κ³  메세지가 λ°œμƒν•  것이닀.

 

Warning: Input: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop.

 

​

refλŠ” prop이 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— undefine으둜 μ„€μ •λœλ‹€λŠ” κ²½κ³  메세지 이닀.

​

μš°λ¦¬κ°€ μ›ν•˜λŠ” μƒμœ„ μ»΄ν¬λ„ŒνŠΈμ—μ„œ ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈμ˜ ref λ₯Ό 뢈러였기 μœ„ν•΄μ„œ ν•„μš”ν•œ κ°œλ…μ΄ ForwardRef κ°€ λ˜λŠ” 것이닀.

​

μœ„λ₯Ό 정상 μž‘λ™ν•˜λŠ” μ½”λ“œλ‘œ 변경해보면 μ•„λž˜ 처럼 이루어진닀.

 

import React, { forwardRef, useRef } from "react";

const Input = forwardRef((props, ref) => {
  return <input type="text" ref={ref} />;
});

function Field() {
  const inputRef = useRef(null);

  function handleFocus() {
    inputRef.current.focus();
  }

  return (
    <>
      <Input ref={inputRef} />
      <button onClick={handleFocus}>μž…λ ₯λž€ 포컀슀</button>
    </>
  );
}

 

 

μ‚¬μš©λ²•

 

1. λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈμ—μ„œ useRef() λ₯Ό import 및 μ„ μ–Έν•΄μ£Όκ³ , μžμ‹ μ»΄ν¬λ„ŒνŠΈλ‘œ 보낸닀.

import React, { useRef } from "react";
import Input from "./child.js"

function Field() {
  const inputRef = useRef(null);

  function handleFocus() {
    inputRef.current.focus();
  }

  return (
    <>
      <Input ref={inputRef} />
      <button onClick={handleFocus}>μž…λ ₯λž€ 포컀슀</button>
    </>
  );
}

 

2. μžμ‹ μ»΄ν¬λ„ŒνŠΈμ˜ 전체λ₯Ό forwardRef() 둜 감싼닀.

ν˜•νƒœλŠ” forwardRef((props,ref)=>{ }); 이며 () 의 κ°’ ν˜•νƒœλŠ” 고정이닀.

λ”°λΌμ„œ propsλ₯Ό λ°›μ•„μ˜¬λ•ŒλŠ” props.____ 둜 받아와야 ν•œλ‹€.

 

import {forwardRef} from "React"

export const Input = forwardRef((props, ref) => {
  return <input type="text" ref={ref} />;
});

 

UseImperativeHandle?

 

λΆ€λͺ¨μ—μ„œ μ •μ˜ν•œ ν•¨μˆ˜λ₯Ό μ•„λž˜λ‘œ λ‚΄λ¦¬λŠ” 것은 ν”ν•œ 일이닀.

ν•˜μ§€λ§Œ μ½”λ“œ μƒμ˜ 이유둜 μΈν•˜μ—¬ μžμ‹μ˜ ν•¨μˆ˜λ₯Ό λΆ€λͺ¨λ‘œ 보내렀면?

react μ—μ„œλŠ” useImperativeHandle μ΄λΌλŠ” κΈ°λŠ₯을 μ‚¬μš©ν•΄μ£Όμ–΄μ•Ό ν•œλ‹€.

​

μ‚¬μš©λ²•

1. forwardRef() κΉŒμ§€ μ…‹νŒ… 된 μƒνƒœμ—μ„œ λΆ€λͺ¨μ—μ„œ μ‚¬μš©ν•  ν•¨μˆ˜λ₯Ό useImperativeHandle() 둜 감싸쀀닀.

import "./styles.css";
import React, { forwardRef, useImperativeHandle, useRef } 
from "react";
/* 1. ν•„μš”ν•œ hook import ν•˜κΈ° */

const Input = forwardRef((props, ref) => {

  /* 3. 받은 동아쀄 ν†΅ν•˜μ—¬ λΆ€λͺ¨μ—κ²Œ 전달해주고 싢은 것듀 
λ°”κ΅¬λ‹ˆμ— 집어 λ„£κΈ°*/
  useImperativeHandle(ref, () => {
    childfunction
  });

  const childfunction = () => {
    console.log("I'm child function");
  };

  return <input type="text" ref={ref} />;
});

export default function App() {
  const inputRef = useRef(null);

  /* 4. λ™μ•—μ€„λ‘œ 전달 받은 λ°”κ΅¬λ‹ˆ 속 λ¬Όν’ˆ μ‚¬μš©ν•˜κΈ° */
  function handleFocus() {
    inputRef.current.childfunction();
  }

  return (
    <>
      /* 2. μžμ‹ 폼으둜 동앗쀄 λ‚΄λ €μ£ΌκΈ° */
      <Input ref={inputRef} />
      <button onClick={handleFocus}>μž…λ ₯λž€ 포컀슀</button>
    </>
  );
}

 

 

이런 κΈ°λŠ₯을 ν†΅ν•˜μ—¬ μ μ ˆν•œ κ³³μ—μ„œ funcμ΄λ‚˜ λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜κ³  κ΄€λ¦¬ν•΄μ£ΌλŠ” 것이 ν•„μš”ν•˜λ©°,

이런 hook을 μ‚¬μš©ν•˜λ©΄ μ–‘λ°©ν–₯ μ†Œν†΅μ΄ μ•„λ‹Œ, 단방ν–₯ μ†Œν†΅μœΌλ‘œ 관리가 κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— μˆœν™˜μ°Έμ‘° 문제λ₯Ό ν•΄κ²°ν•  수 있게 λœλ‹€.!

 


​곡식 λ¬Έμ„œλ₯Ό μ½κ±°λ‚˜ κ°œλ°œν•˜λ©° λŠκΌˆλ˜ λ‡Œν”Όμ…œλ‘œ μž‘μ„±ν•˜κ³  μžˆμœΌλ‹ˆ, 
κΆκΈˆν•˜μ‹  μ μ΄λ‚˜ μž˜λͺ»λœ μ μ΄ μžˆλ‹€λ©΄ λŒ“κΈ€ λ‚¨κ²¨μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€.

728x90
더보기
Document/λ¨•μ„ μƒμ˜ 코딩ꡐ싀