[英]React HOC, instanceof, wrapped with?
I use HOC and the compose method from Redux to wrap a component in components. 我使用HOC和Redux的compose方法将组件包装在组件中。 A simple example :
一个简单的例子:
const Textinput = class Textinput extends Component {
...
}
export default compose(
Theme,
Validator,
)(Textinput);
The result rendered by react is : react呈现的结果是:
<theme>
<validator>
<textinput />
Imagine another component with this children : 想象一下这个孩子的另一个组成部分:
<Textinput />
<span>Test</span>
<Select />
<br/>
Textinput and Select are wrapped in the Theme/Validator components with HOC. Textinput和Select通过HOC包装在Theme / Validator组件中。 I want to enumerate each element in this component :
我想枚举此组件中的每个元素:
const children = React.Children.map(this.props.children, (child) => {
}
But : 但是:
how to know if a rendered element is wrapped in a specific component (like Validator or Theme) ? 如何知道渲染的元素是否包装在特定组件(如Validator或Theme)中?
how to know if the component behind an element is an instanceof Select or Textinput (and not Theme) ? 如何知道元素后面的组件是Select还是Textinput的instance(而不是Theme)?
Another solution is to add a new property on each HOC object like : 另一种解决方案是在每个HOC对象上添加一个新属性,例如:
Theme.innerInstances = {
instance: WrappedComponent,
innerInstances: WrappedComponent.innerInstances,
};
Validator.innerInstances = {
instance: WrappedComponent,
innerInstances: WrappedComponent.innerInstances,
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.