Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

javascript - Converting Class based js component to typescript component with dynamic props destructuring

I have a class based component like this

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. This is the best I came up with in 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 }

The error is Expression expected.ts(1109)

In javascript code this the respectful line is {...this.props}

How can I convert my javascript class to typescript ?

And am I correct with the way I converted this line {...this.props.style} in javascript code ?

question from:https://stackoverflow.com/questions/65896257/converting-class-based-js-component-to-typescript-component-with-dynamic-props-d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Inside a prop list, doing

{...this.props}

will extract each individual property from the object and list it as an object. If this.props is { foo: 'foo' }, for example, then

{...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:

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):

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...