简体   繁体   English

参考原始DOM元素类

[英]Refer to primitive DOM element class

👋 👋

For example, I want to do this: 例如,我要这样做:

const DynamicComponent = condition ? MyComponent : React.div;

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

What do I use there instead of React.div , which is a thing I just made up? 我在这里用什么代替React.div呢?

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

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

You can access the this.props.children to make a layout kind of thing. 您可以访问this.props.children来进行布局。 You can access the child of some component inside itself by using this.props.children . 您可以使用this.props.children访问其内部某个组件的子this.props.children

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

This will wrap anything you put inside the Div tag between the HTML div tag. 这会将您放入Div标签内的所有内容包装在HTML div标签之间。 To access the props of the component given to the <Div> , you can simply add {...this.props} , it will add all your props on the actual div . 要访问赋予<Div>的组件的{...this.props} ,只需添加{...this.props} ,它将在实际div上添加所有{...this.props}

You can use the above-given component like: 您可以使用上述组件,例如:

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

will convert to: 将转换为:

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

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

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