React的子组件有未定义的props.style

React Child component has undefined props.style

本文关键字:props style 未定义 组件 React      更新时间:2023-09-26

我是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试图谷歌这个问题,但一无所获。有人能帮帮我吗?由于

编辑:

迈克尔的回答被发现了。问题是在我的StackView组件中,我在克隆它们时重写了子样式

我敢肯定ContainerClickableText组件的父组件有问题。

请检查父组件StackView。如果你重写了children样式,那么这就是问题的根源。

好运。

如果你的组件不是无状态功能组件,那么你需要使用this.props.style来访问该属性。

更多的组件类型在这里:https://tylermcginnis.com/functional - - vs -无状态组件功能组件- vs -无状态组件- 630 fdfd90c9c