当前位置: 首页 > 知识库问答 >
问题:

React中用于Rest道具的TypeScript工作区

黄弘深
2023-03-14

TypeScript 2.1现在支持对象扩展/静止,因此不再需要任何解决方法!

TypeScript支持JSX扩展属性,该属性通常用于React,以将HTML属性从组件传递到呈现的HTML元素

interface LinkProps extends React.HTMLAttributes {
  textToDisplay: string;
}

class Link extends React.Component<LinkProps, {}> {
  public render():JSX.Element {
    return (
      <a {...this.props}>{this.props.textToDisplay}</a>
    );
  }
}

<Link textToDisplay="Search" href="http://google.com" />

但是,如果您向超文本标记语言元素传递任何未知的道具,React会引入警告。上面的例子会产生一个React运行时警告,text ToDisplay的未知道具。

const {textToDisplay, ...htmlProps} = this.props;
return (
  <a {...htmlProps}>{textToDisplay}</a>
);

但是TypeScript还不支持这种语法。我知道,希望有一天我们能用打字脚本完成这项工作 (更新:TS 2.1现在支持对象扩展/Rest!为什么你还在读这个??)在此期间,有哪些变通办法?我正在寻找一种不会影响类型安全性的解决方案,并发现它异常困难。例如,我可以这样做:

const customProps = ["textDoDisplay", "otherCustomProp", "etc"];
const htmlProps:HTMLAttributes = Object.assign({}, this.props);
customProps.forEach(prop => delete htmlProps[prop]);

但这需要使用字符串属性名,而这些属性名不会根据实际的道具进行验证,因此容易出现拼写错误和糟糕的IDE支持。有没有更好的办法?


共有3个答案

潘刚洁
2023-03-14

使用... rest

type ButtonProps = {
    disabled: boolean;
};

function Button(props: ButtonProps): JSX.Element {
    const {disabled = false, ...rest} = props;
...
return (
    <button disabled={disabled} {...rest}>
....
田镜
2023-03-14

您可能无法避免使用this.props属性的子集创建新对象,但是您可以使用类型安全来实现这一点。

例如:

interface LinkProps {
    textToDisplay: string;
}

const LinkPropsKeys: LinkProps = { textToDisplay: "" };

class Link extends React.Component<LinkProps & React.HTMLAttributes, {}> {
    public render(): JSX.Element {
        return (
            <a { ...this.getHtmlProps() }>{ this.props.textToDisplay }</a>
        );
    }

    private getHtmlProps(): React.HTMLAttributes {
        let htmlProps = {} as React.HTMLAttributes;

        for (let key in this.props) {
            if (!(LinkPropsKeys as any)[key]) {
                htmlProps[key] = this.props[key];
            }
        }

        return htmlProps;
    }
}

使用需要与LinkProps匹配的LinkPropsKeys对象将帮助您保持界面和运行时查找之间的键同步。

林弘文
2023-03-14

这实际上比上面所有的答案都简单。您只需遵循以下示例:

type Props = {
  id: number,
  name: string;
  // All other props
  [x:string]: any;
}

const MyComponent:React.FC<Props> = props => {
  // Any property passed to the component will be accessible here
}

希望这有帮助。

 类似资料:
  • 我学习打字稿。我想设置组件功能道具类型。但我不知道如何设置。 这是代码。 我不想设置“任何”。我设置了什么类型?

  • 问题内容: 我有一个非常基本的有状态组件,在其中我使用recompose将多个HOC添加到我的组件中(在我的示例中,为简单起见,我只使用了一个)。出于某种原因,打字稿给我关于我的道具进入组件的错误。如何摆脱这个错误? 这是我的代码: 我得到的错误是: 如果我不使用重组,而是写 我没有任何错误。 问题答案: 通过compose的类型输入,您可以指定结果组件的类型以及可以调用的组件的类型,因此可以避免

  • 在React-Typescript初学者示例“创建组件”的“创建组件”部分中,Typescript中有一个基本的React组件: 我是打字新手。似乎Typescript使用接口道具来执行道具类型检查(类似于Proptypes npm包所做的)。因此,问题是: 如果我已经在使用这种TypeScript接口语法do to props类型检查,我还需要在同一个组件中使用像这样的Proptype包吗?

  • 我使用的是dev-express中的react-grid库,库中有一个表组件,我们可以向它传递单元格组件,在CustomCell中,我使用的是Material UI中的菜单 在上述情况下,菜单工作良好, 但是我想要传递道具到这个组件,我尝试了以下操作 在这种情况下菜单不工作,我想知道是否有一个替代的方法来实现第二种情况。

  • 在Tabs组件中,具有以下属性 儿童[0](this.props.children)将看起来像 孩子[0]。道具看起来像 我希望属性如下所示。上面只是打印出语句函数。 这是一个奇怪的问题,但说来话长,我正在使用一个库,这就是它的结论。