在TypeScript中将forwardRef组件与子代一起使用

时间:2019-02-12 16:12:11

标签: reactjs typescript jsx tsx

使用@ types / react 16.8.2和TypeScript 3.3.1。

我直接从React documentation提出了这个前向引用示例,并添加了一些类型参数:

const FancyButton = React.forwardRef<HTMLButtonElement>((props, ref) => (
  <button ref={ref} className="FancyButton">
    {props.children}
  </button>
));

// You can now get a ref directly to the DOM button:
const ref = React.createRef<HTMLButtonElement>();
<FancyButton ref={ref}>Click me!</FancyButton>;

FancyButton下的最后一行中出现以下错误:

  

类型'{ children: string; ref: RefObject<HTMLButtonElement>; }'不是   可分配给类型“ IntrinsicAttributes & RefAttributes<HTMLButtonElement>”。属性“ children”不   存在于类型'IntrinsicAttributes & RefAttributes<HTMLButtonElement>'。ts(2322)

似乎React.forwardRef的返回值的类型定义是错误的,没有正确地合并在子道具中。如果我使<FancyButton>自动关闭,则错误消失。缺少针对该错误的搜索结果,使我相信我缺少明显的东西。

3 个答案:

答案 0 :(得分:3)

trevorsg,您需要传递按钮属性:

import * as React from 'react'

type ButtonProps = React.HTMLProps<HTMLButtonElement>

const FancyButton = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => (
  <button type="button" ref={ref} className="FancyButton">
    {props.children}
  </button>
))

// You can now get a ref directly to the DOM button:
const ref = React.createRef<HTMLButtonElement>()

<FancyButton ref={ref}>Click me!</FancyButton>

已添加:

在TS和@ types / react的最新版本中,您也可以使用React.ComponentPropsWithoutRef<'button'>代替React.HTMLProps<HTMLButtonElement>

答案 1 :(得分:3)

aMarCruz 和 euvs 给出的答案都有效,但它们对消费者有点撒谎。他们说他们接受所有 HTMLButtonElement 道具,但他们忽略它们而不是将它们转发到按钮。如果您只是想正确地合并 children 道具,那么您可能想改用 React.PropsWithChildren:

import React from 'react';

interface FancyButtonProps {
    fooBar?: string; // my custom prop
}

const FancyButton = React.forwardRef<HTMLButtonElement, React.PropsWithChildren<FancyButtonProps>>((props, ref) => (
    <button type="button" ref={ref} className="fancy-button">
        {props.children}
        {props.fooBar}
    </button>
));

FancyButton.displayName = 'FancyButton';

或者显式添加一个 children 属性:

interface FancyButtonProps {
    children?: React.ReactNode;
    fooBar?: string; // my custom prop
}

const FancyButton = React.forwardRef<HTMLButtonElement, FancyButtonProps>((props, ref) => (
    <button type="button" ref={ref} className="fancy-button">
        {props.children}
        {props.fooBar}
    </button>
));

FancyButton.displayName = 'FancyButton';

或者如果你真的想接受所有的按钮道具并转发它们(例如让消费者选择按钮类型=“提交”),那么你可能想要使用rest/spread:

import React from 'react';

interface FancyButtonProps extends React.ComponentPropsWithoutRef<'button'> {
    fooBar?: string; // my custom prop
}

const FancyButton = React.forwardRef<HTMLButtonElement, FancyButtonProps>(
    ({ children, className = '', fooBar, ...buttonProps }, ref) => (
        <button {...buttonProps} className={`fancy-button ${className}`} ref={ref}>
            {children}
            {fooBar}
        </button>
    ),
);

FancyButton.displayName = 'FancyButton';

答案 2 :(得分:0)

aMarCruz给出的答案很好用。但是,如果您还需要将自定义道具传递给FancyButton,则可以按照以下步骤进行。

interface FancyButtonProps extends React.ComponentPropsWithoutRef<'button'> {
    fooBar?: string; // my custom prop
}

const FancyButton = React.forwardRef<HTMLButtonElement, FancyButtonProps>((props, ref) => (
    <button type="button" ref={ref} className="FancyButton">
        {props.children}
        {props.fooBar}
    </button>
));


/// Use later

// You can now get a ref directly to the DOM button:
const ref = React.createRef<HTMLButtonElement>()

<FancyButton ref={ref} fooBar="someValue">Click me!</FancyButton>

只需在此处添加即可完成。