简体   繁体   English

将类分配给React中自定义组件的外部包装器

[英]assign class to outer wrapper of a customized component in React

How can we assign a class to the outmost wrapper of a component in React? 我们如何在React的组件的最外包装中分配一个类?

For example, I was trying to add class "table" to this "FilterableProductTable" so that the outmost wrapper, "" in this case, can have a class assigned to it? 例如,我试图将类“ table”添加到此“ FilterableProductTable”中,以便在这种情况下,最外层的包装“”可以分配一个类?

What I did is to add prop "className={"table"}", but it does not seem to work. 我所做的是添加prop“ className = {” table“}”,但是它似乎不起作用。 When inspected, the outmost div still doesn't have a class. 经过检查,最外面的div仍然没有课程。

 ReactDOM.render( <FilterableProductTable className={'table'} products={PRODUCTS} />, document.getElementById("container") ); 

the outmost div 最远的div

the outer wrapper div still doesn't have a class 外包装div仍然没有类

This is completely possible. 这是完全可能的。 You must remember that FilterableProductTable is a Component and doesn't represent a DOM node element, like div , so you are actually just passing a property called className to your Component. 您必须记住FilterableProductTable是一个Component,并且不代表div类的DOM节点元素,因此实际上您只是将一个名为className的属性传递给Component。 It is then up to your component to use the passed in property. 然后由您的组件使用传递的属性。

For eg 例如

class FilterableProductTable extends Component {
  render() {
    return (
      <div className={this.props.className}>
        ...
      </div>
    );
  }
}

ReactDOM.render(
  <FilterableProductTable className='table' products={PRODUCTS} />,
  document.getElementById("container")
);

If you notice above I am actually accessing the passed in property called className via this.props in the FilterableProductTable . 如果您发现上面我其实在访问属性调用传递className通过this.propsFilterableProductTable So any properties you set on a Component when rendering it will be passed into the Component, and then it is up to the Component's own rendering logic how it wants to use those properties. 因此,在渲染时在组件上设置的任何属性都将传递到组件中,然后由组件自己的渲染逻辑决定如何使用这些属性。

You could have called the property foo or whatever too, for eg: 您可能也将属性称为foo或其他名称,例如:

class FilterableProductTable extends Component {
  render() {
    return (
      <div className={this.props.foo}>
        ...
      </div>
    );
  }
}

ReactDOM.render(
  <FilterableProductTable foo='table' products={PRODUCTS} />,
  document.getElementById("container")
);

Look at the React docs, all the following are HTML Elements natively supported by React: https://facebook.github.io/react/docs/tags-and-attributes.html#html-elements 查看React文档,以下所有都是React本机支持的HTML元素: https : //facebook.github.io/react/docs/tags-and-attributes.html#html-elements

If you set the className prop on those items then the class would be attached to them directly. 如果在这些项目上设置className属性,则该类将直接附加到它们。 :) :)

On the outermost div of the FilterableProductTable component you need to include the className prop: FilterableProductTable组件的最外面的div上,您需要包括className属性:

<div className={this.props.className}>
    {...children}
</div>

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

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