【问题标题】:Spread types may only be created from object types传播类型只能从对象类型创建
【发布时间】:2018-07-21 03:42:33
【问题描述】:

我有一个使用 ts 的简单 react-native 组件,它指定了它自己的样式,但是我也想从组件树的上方(即我使用这个组件的地方)传递我可能在它上面使用的任何样式

class RatioImage extends React.Component<Props> {
  render() {
    const { width, ratio, style, ...props } = this.props;
    return (
      <Image
        {...props}
        style={{
          width: deviceWidth * (width / 100),
          height: deviceWidth * (width / 100) * ratio,
          ...style
        }}
      />
    );
  }
}

我目前在我的...style 上遇到错误,不知道为什么,因为它应该是一个对象?

[ts] Spread types may only be created from object types.
const style: false | ImageStyle | (number & {
    __registeredStyleBrand: ImageStyle;
}) | RecursiveArray<false | ImageStyle | (number & {
    __registeredStyleBrand: ImageStyle;
}) | null | undefined> | null | undefined

【问题讨论】:

  • style 是一个对象吗?

标签: reactjs typescript react-native ecmascript-6 spread-syntax


【解决方案1】:

你需要使用数组语法在 react native 中提供多种样式

<Image style={[{ width: x, height; y}, style]} />

the docs for more info

【讨论】:

    猜你喜欢
    • 2019-09-10
    • 2021-08-02
    • 2018-12-13
    • 2020-08-02
    • 2020-05-26
    • 1970-01-01
    • 2020-02-07
    • 2021-12-31
    相关资源
    最近更新 更多