@types/16.8.17
ν¨ν€μ§λ₯Ό μ¬μ©ν΄ 보μλλ° λ¬Έμ κ° μμμ΅λλ€.Type 'MutableRefObject<HTMLDivElement | undefined>' is not assignable to type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.
Type 'MutableRefObject<HTMLDivElement | undefined>' is not assignable to type 'RefObject<HTMLDivElement>'.
Types of property 'current' are incompatible.
Type 'HTMLDivElement | undefined' is not assignable to type 'HTMLDivElement | null'.
Type 'undefined' is not assignable to type 'HTMLDivElement | null'.
νΈμ§ 2: λ΄ μ루μ μ 무μνκ³ μλ μ루μ μ μ¬μ©νμΈμ. :)
λλ νμ¬ κ°μ λ¬Έμ κ° μμ΅λλ€. νΉμ μ΄λ―Έ ν΄κ²°μ± μ μ°Ύμμ΅λκΉ?
νΈμ§ : λλ μ§κΈ μ΄κ²μ μ¬μ©νκ³ μμ΅λλ€. λΆλͺ ν κ·Έλ κ² μμμ§λ μμ΅λλ€. ν΄κ²°μ± :
const inputField = React.useRef() as React.MutableRefObject<HTMLInputElement>;
μ¬κΈ°μμ μ°Ύμ μ μμ΅λλ€: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/28884#issuecomment -471341041
μ½κ° κΉλν ν΄κ²°μ± μ useRef ν¨μ νΈμΆμμ initialValueλ₯Ό nullλ‘ μ€μ νλ κ²μ λλ€.
const componentRef = useRef<HTMLDivElement>(null);
μ΄κ²μ componentRef = HTMLDivElement | null
μ 보μ₯ν©λλ€. μ΄λ HTMLDivElement | undefined
보λ€λ ref μνμ΄ κΈ°λνλ κ²μ
λλ€.
@shane935 λ μ€λλ μκ³ νμ ¨μ΅λλ€ ππ»
λ€μκ³Ό κ°μ΄ undefined
λ₯Ό RefObject<T>.current
μ μΆκ°ν΄μΌ ν©λλ€.
//index.ts, line 80
interface RefObject<T> {
readonly current: T | null;
}
κ·Έλμ μ½λλ€
//index.ts, line 80
interface RefObject<T> {
readonly current: T | null | undefined;
}
λ²κ·Έκ° μλλΌ ν©λ²μ μΈ μ λ ₯ μ€λ₯λ₯Ό μ‘λ κ²μ λλ€.
μμΈν λ΄μ©μ https://github.com/DefinitelyTyped/DefinitelyTyped/pull/38228#issuecomment -529749802λ₯Ό μ°Έμ‘°νμΈμ.
@Jessidhia κ° λ§νλ―μ΄ λ²κ·Έκ° μλλλ€. μλ§λ λ€μκ³Ό κ°μ΄ μμ±ν΄μΌ ν κ²μ λλ€.
import React, {
forwardRef,
ForwardRefRenderFunction,
useImperativeHandle,
useRef,
PropsWithChildren,
} from "react";
interface Props {}
interface Ref {
value: string;
setValue: (value: string) => void;
}
const InputEmail: ForwardRefRenderFunction<Ref, PropsWithChildren<Props>> = (
props,
ref
) => {
// hooks
const inputElement = useRef<HTMLInputElement | null>(null);
useImperativeHandle(ref, () => {
return {
value: inputElement.current ? inputElement.current.value : "",
setValue: (value: string) => {
inputElement.current && (inputElement.current.value = value);
},
};
});
// render
return (
<div>
<input
type="text"
ref={inputElement}
defaultValue="[email protected]"
disabled
/>
</div>
);
};
const Component = forwardRef(InputEmail);
export default Component;
μ΄κ²μ΄ λͺ¨λ μ¬λμ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμμ§ νμ ν μ μμΌλ©° μ΄ λ¬Έμ μ λν μ λ¬Έκ°λ μλμ§λ§ λ΄κ° refλ‘ μ λ¬ν λ΄μ©κ³Ό refλ₯Ό μ λ¬ν κ΅¬μ± μμμ μ€μ ref μ νμ μ΄ν΄λ³Έ ν , μΈν°νμ΄μ€κ° μλͺ»λ κ² κ°μμ΅λλ€.
μ²μμλ μμ κ΅¬μ± μμμ λν΄ λ€μκ³Ό κ°μ μν μΈν°νμ΄μ€κ° μμμ΅λλ€.
type TextboxProps = CustomProps & React.HTMLProps<HTMLInputElement>
λ΄κ° λ³κ²½ν κ²:
type TextboxProps = CustomProps & React.HTMLAttributes<HTMLInputElement>
κ·Έλ° λ€μ μ λ ₯ μ€λ₯κ° μ¬λΌμ‘μ΅λλ€.
λλ³΄λ€ λ λ§μ΄ μλ μ² νμ§λ μκ² μ§λ§ HTMLProps μΈν°νμ΄μ€μ React.forwardRef() ν¨μ μ νμ μ νμ μ΄ν΄λ³΄λ©΄ RefAttributesλ₯Ό μ¬μ©νκ³ μμμ μ μ μμ΅λλ€.
μ΄ νλ‘μ νΈμλ useRef()
μ λν΄ 3κ°μ μ€λ²λ‘λκ° μλ κ² κ°μ΅λλ€. 3κ°κ° λͺ¨λ νμν©λκΉ? useRef<T>(null)
μΌμ΄μ€λ κ°λ°μκ° κ°μ₯ λ§μ΄ μ¬μ©νλ μΌμ΄μ€μΈ κ² κ°μ΅λλ€.
function useRef<T>(initialValue: T): MutableRefObject<T>;
function useRef<T>(initialValue: T|null): RefObject<T>;
function useRef<T = undefined>(): MutableRefObject<T | undefined>;
RefObject
μ΄ μμ΅λλ€.
ref
μνμΌλ‘ Panel
κ΅¬μ± μμμ μ λ¬ν©λλ€.
w/ forwardRef
μλΉνλ €κ³ νλ©΄ MutableRefObject
λ©λλ€.
μ΄μ .current
μμ±μ μ¬μ©ν μ μμ΅λλ€.
@Jessidhia @osf2e κ·νμ μ μμ μ¬κΈ°μ ν΄λΉλμ§ μλ κ² κ°μ΅λλ€.
λ΄κ° μμ§ν΄μΌ νλ ν΄κ²° λ°©λ²:
if (ref && (ref as RefObject<HTMLDivElement>).current) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(ref as RefObject<
HTMLDivElement
>)!.current!.style.height = `${containerHeight}px`;
}
react-native-webμ μ¬μ©νμ¬ μ¬μ©ν΄μΌνμ΅λλ€.
import {RefObject} from 'react';
// ...
const myRef = useRef() as RefObject<View>
Input
μμμ λν Refμ μ΄ λ¬Έμ κ° μμμ΅λλ€.
κ·Έλμ useRef<HTMLDivElement>()
-> useRef<HTMLInputElement>()
μμ μ ννλλ° μ μμ μΌλ‘ μλνμ΅λλ€.
@joshribakoff 맀κ°λ³μλ₯Ό μ§μ μ λ ₯ν μλ μμ΅λλ€.
export const Panel = React.forwardRef((props: PanelProps, ref: React.RefObject<HTMLDivElement>) => {
κ·Έλ κ² νλ©΄ refμ μ‘μΈμ€ν λ typecastν νμκ° μμ΅λλ€.
κ·Όλ° μ μ΄κ±Έ ν΄μΌνλμ§ μμ§λ μ΄ν΄κ° μκ°...
κ°μ₯ μ μ©ν λκΈ
μ½κ° κΉλν ν΄κ²°μ± μ useRef ν¨μ νΈμΆμμ initialValueλ₯Ό nullλ‘ μ€μ νλ κ²μ λλ€.
const componentRef = useRef<HTMLDivElement>(null);
μ΄κ²μ componentRef =
HTMLDivElement | null
μ 보μ₯ν©λλ€. μ΄λHTMLDivElement | undefined
보λ€λ ref μνμ΄ κΈ°λνλ κ²μ λλ€.