您好,欢迎来到暴趣科技网。
搜索
您的当前位置:首页React错误状态

React错误状态

来源:暴趣科技网

今天使用React写组件的时候突然发现了一个报错:
React does not recognize the marginTop prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase margintop instead. If you accidentally passed it from a parent component...

然后就开始疯狂的找原因,是因为将不属于DOM的组件属性传给了DOM,

interface StylePorps {
  buttonWidth?: string;
};

interface StylePropsAfter extends StyleProps {
  marginLeft?: string;
  border?: string;
}

const Button: FC<StylePorps> => ({
  buttonWidth = '180px',
  ...props
}) => (
  <div buttonWidth={buttonWidth} {...props}>
    <button {...props} />
  </div>
)

最后解决方法,将属性分离就好了:

interface StylePorps {
  buttonWidth?: string;
  marginLeft?: string;
};

interface StylePropsAfter extends StyleProps {
  border?: string;
}

const Button: FC<StylePropsAfter> => ({
  buttonWidth = '180px',
  marginLeft = '20px'
  ...props
}) => (
  <div buttonWidth={buttonWidth} marginLeft={marginLeft} {...props}>
    <button {...props} />
  </div>
)

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoquwan.com 版权所有 湘ICP备2024080961号-7

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务