繁体   English   中英

参考原始DOM元素类

[英]Refer to primitive DOM element class

👋

例如,我要这样做:

const DynamicComponent = condition ? MyComponent : React.div;

const usedDynamicComponent = <DynamicComponent> How cool? </DynamicComponent>;

我在这里用什么代替React.div呢?

const DynamicComponent = condition ? MyComponent : <div>{this.props.children}</div>;

const usedDynamicComponent = <DynamicComponent> How cool? </DynamicComponent>;

您可以访问this.props.children来进行布局。 您可以使用this.props.children访问其内部某个组件的子this.props.children

class Div extends Component {
  render() {
    return <div {...this.props} >{this.props.children}</div>;
  }
}

这会将您放入Div标签内的所有内容包装在HTML div标签之间。 要访问赋予<Div>的组件的{...this.props} ,只需添加{...this.props} ,它将在实际div上添加所有{...this.props}

您可以使用上述组件,例如:

<Div className="custom" custom_prop="value"><AnyReactComponent /></Div>

将转换为:

<div className="custom" custom_prop="value"><AnyReactComponent /></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM