javascript - 你怎么称呼这个 React 组件模式?

标签 javascript reactjs styled-components

我很难找到有关此模式的一些文档。 有名字吗?

TextBase 是一个样式组件。所以我可以将其扩展如下: Text.H1 = withComponent('h1') 但是我也希望传递 html 属性。因此就有了函数组件。但是,当我扩展 Text 组件时, Prop 将被覆盖,导致所有组件都是 h1 的。

const Text = (props) => {
   const { children, testid, ...rest } = props;
   return <TextBase data-testid={testid} {...rest}>{children}</TextBase>
}
Text.defaultProps = {color: 'red'}

Text.H1 = Text
Text.H1.defaultProps = { as: 'h1'}

Text.H2 = Text
Text.H2.defaultProps = { as: 'h2'}

Text.H3 = Text
Text.H3.defaultProps = { as: 'h3'}

🙏🏽

最佳答案

尝试使用 this 绑定(bind)函数调用或使用箭头函数或使用 bind 手动绑定(bind)函数。我确信它会起作用。引用错误就是这一切

关于javascript - 你怎么称呼这个 React 组件模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57390747/

相关文章:

reactjs - 如何使用reactjs、redux和redux-simple-router根据auth状态共享根URL?

javascript - 在 Enzyme 描述函数中的何处挂载和卸载?

javascript - 在包装器中设置样式化组件的样式

javascript - 计算字符串中 2 个指定单词之间的字符数

javascript - 变量中的函数名

javascript - 新函数字符串到真实 javascript 的替代选项

reactjs - 如何更新 React Router 中的路由而不在单页应用程序中重新安装组件?

javascript - 似乎无法通过 bool 值设置我的组件的样式

javascript - React.forwardRef 导致样式组件错误

javascript - for循环中的Ajax请求和索引顺序