javascript - 防止在样式组件中将 Prop 从子级传递给父级

标签 javascript reactjs typescript inheritance styled-components

我在 TypeScript 中使用样式化组件库。我在创建继承自 React 组件 A 的样式化组件 B 时遇到问题。 A 是 node_module(我无法更改 A 的行为)。但是 A 将所有属性传递给 div

如果 B 有任何 A 没有的属性,控制台会显示警告消息,因为 div 没有属性 已展开:

Warning: React does not recognize the isExpanded prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase isexpanded instead. If you accidentally passed it from a parent component, remove it from the DOM element.

interface AProps {

}

interface BProps {
    isExpanded: boolean
}

const A = (props: AProps) => (
    <div {...props} />
) // Component A pass all props to <div>

const B = Styled(A)<BProps>`

` // I need isExpaned prop in component B and I would like to interit from A

有什么方法可以防止样式化组件中子级到父级的“冒泡” Prop ?

最佳答案

样式组件中没有针对此的内置解决方案,但您可以将其包装到函数组件中并解构不需要的属性。

const B = Styled(({isExpanded, ...props})=><A {...props}/>)<BProps>`

您还可以查看有关此主题的 github-issue:https://github.com/styled-components/styled-components/issues/135

关于javascript - 防止在样式组件中将 Prop 从子级传递给父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57230862/

相关文章:

javascript - 将所有动态添加的文本区域值合并到一个文本区域

javascript - 我怎样才能得到一个会改变多个 div 的 jquery 幻灯片插件?

reactjs - next/dynamic 中无效的悬念选项使用

angular - 按每个单词的第一个字母过滤包含元素的数组

typescript - 如何在 Angular 2 中将 "String"值转换为 "Type"

javascript - 如何申请:loading in dynamically created button through v-for in Vue js

javascript - 如何让 webglcontextrestored 被解雇

html - 如何使用 css、html 绘制多条对 Angular 线 [right hash lines]?

django - 如何将 ReactJS Form 连接到 Django 模型以自动获取字段(外键、选项等)

javascript - 类型 'number' 不可分配给类型 'ReadonlyArray<{}>'