简体   繁体   English

使用动态道具解构将基于 Class 的 js 组件转换为 typescript 组件

[英]Converting Class based js component to typescript component with dynamic props destructuring

I have a class based component like this我有一个像这样的基于 class 的组件

class ArInput extends React.Component {
  render() {
    const { shadowless, success, error } = this.props;

    const inputStyles = [
      styles.input,
      !shadowless && styles.shadow,
      success && styles.success,
      error && styles.error,
      {...this.props.style}
    ];

    return (
      <Input
        placeholder="write something here"
        placeholderTextColor={argonTheme.COLORS.MUTED}
        style={inputStyles}
        color={argonTheme.COLORS.HEADER}
        iconContent={
          <Icon
            size={14}
            color={argonTheme.COLORS.ICON}
            name="link"
            family="AntDesign"
          />
        }
        {...this.props}
      />
    );
  }
}

I'm trying to convert above class to functional based component with react hooks.我正在尝试将上面的 class 转换为带有反应钩子的基于功能的组件。 This is the best I came up with in typescript这是我在 typescript 中想出的最好的

const ArInput =({ shadowless, success, error, style }:any)=> {

    const inputStyles = [
      styles.input,
      !shadowless && styles.shadow,
      success && styles.success,
      error && styles.error,
      {...style}
    ];

    return (
      <Input
        placeholder="write something here"
        placeholderTextColor={argonTheme.COLORS.MUTED}
        style={inputStyles}
        color={argonTheme.COLORS.HEADER}
        iconContent={
          <Icon
            size={14}
            color={argonTheme.COLORS.ICON}
            name="link"
            family="AntDesign"
          />
    }
    { ...shadowless, ...success, ...error, ...style }
  />
);

} }

but I'm getting an error on this line {...shadowless, ...success, ...error, ...style }但我在这一行遇到错误{...shadowless, ...success, ...error, ...style }

The error is Expression expected.ts(1109)错误是Expression expected.ts(1109)

In javascript code this the respectful line is {...this.props}在 javascript 代码中,尊敬的行是{...this.props}

How can I convert my javascript class to typescript?如何将我的 javascript class 转换为 typescript?

And am I correct with the way I converted this line {...this.props.style} in javascript code?我在 javascript 代码中转换这一行{...this.props.style}的方式是否正确?

Inside a prop list, doing在道具列表中,做

{...this.props}

will extract each individual property from the object and list it as an object.将从 object 中提取每个单独的属性并将其列为 object。 If this.props is { foo: 'foo' } , for example, then例如,如果this.props{ foo: 'foo' } ,那么

{...this.props}

is equivalent to相当于

foo="foo"

in the props list.在道具列表中。

You want the props to be inside of the <Input props list, so either do something like this:您希望道具位于<Input props 列表中,因此请执行以下操作:

return (
  <Input
    placeholder="write something here"
    placeholderTextColor={argonTheme.COLORS.MUTED}
    style={inputStyles}
    color={argonTheme.COLORS.HEADER}
    iconContent={
      <Icon
        size={14}
        color={argonTheme.COLORS.ICON}
        name="link"
        family="AntDesign"
      />
    }
    shadowless={shadowless}
    success={success}
    error={error}
    style={style}

Or make an object from the props, then spread the object (don't spread the individual props):或者从道具制作一个 object,然后传播object (不要传播单个道具):

return (
  <Input
    placeholder="write something here"
    placeholderTextColor={argonTheme.COLORS.MUTED}
    style={inputStyles}
    color={argonTheme.COLORS.HEADER}
    iconContent={
      <Icon
        size={14}
        color={argonTheme.COLORS.ICON}
        name="link"
        family="AntDesign"
      />
    }
    {...{ shadowless, success, error, style }}

If you're going to use TS, I'd also highly recommend using a more precise type than any - using any effectively disables type-checking for the expression, which defeats the purpose of TS.如果您要使用 TS,我还强烈建议您使用比any更精确的类型 - 使用any会有效地禁用表达式的类型检查,这违背了 TS 的目的。

You can try something like this:你可以尝试这样的事情:

interface ArInputProps{
shadowless:string; //i am assuming all of them to be of type string,you can use boolean,Function or whatever you like
success:string;
error:string;
style:string
}

const ArInput =({ shadowless, success, error, style }:ArInputProps)=> {

    const inputStyles = [
      styles.input,
      !shadowless && styles.shadow,
      success && styles.success,
      error && styles.error,
      {...style}
    ];

    return (
      <Input
        placeholder="write something here"
        placeholderTextColor={argonTheme.COLORS.MUTED}
        style={inputStyles}
        color={argonTheme.COLORS.HEADER}
        iconContent={
          <Icon
            size={14}
            color={argonTheme.COLORS.ICON}
            name="link"
            family="AntDesign"
          />
    }
    shadowless={shadowless}
    success={success}
    error={error}
    style={style}
  />
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM