React 子组件未定义 props.style

React Child component has undefined props.style

我对 React 很陌生。我想创建其中包含多个自定义组件的自定义组件。下面是代码

const { item, to, onClick, width, style, ...otherProps } = props;
const h = width ? (width*IMAGE_RATIO)+'px' : 'auto';
const w = width ? width+'px' : 'auto';
<StackView
  style={{ width: w, ...style }}
  {...otherProps}
>
  <Container
    to={to}
    onClick={onClick}
    item={item}
    style={{ width: w, height: h }}
  />
  <ClickableText
    to={to}
    onClick={onClick}
    typography={typography.t15}
    color={color.red}
    activeColor={color.redDark}
    style={{ lineHeight: '12px' }}
  >
    {item.title}
  </ClickableText>
</StackView>

Container 组件和 ClickableText 组件内 props.style 得到了 undefined 试图 Google 这个问题但没有找到。有人可以帮我吗?谢谢

编辑:

Michael 答案被发现。问题出在我的 StackView 组件中,我在克隆它们时覆盖了子样式

如果您的组件不是无状态功能组件,那么您需要使用 this.props.style.

访问 属性

更多关于组件类型的信息: https://tylermcginnis.com/functional-components-vs-stateless-functional-components-vs-stateless-components-630fdfd90c9c

我确定 ContainerClickableText 组件的父级有问题。

请检查父组件StackView。如果您覆盖子样式,那么这就是这个烂摊子的根本问题。

祝你好运。