How to add a classname/id to React-Bootstrap Component?

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-11 04:08:05

问题


Suppose we are using Row from React-Bootstrap... How do we style it without using a wrapper or inner element:

<Row>
  <div className='some-style'>
   ...
</Row>

Ideally we could just do:

<Row className='some-style> 
  ...
</Row>

But this doesn't work and I'd presume it's because React-Bootstrap does not know where the className goes within the Row component (it should just style the div that has the row's styles I think)


回答1:


If you look at the code for the component you can see that it uses the className prop passed to it to combine with the row class to get the resulting set of classes (<Row className="aaa bbb"... works).Also, if you provide the id prop like <Row id="444" ... it will actually set the id attribute for the element.




回答2:


1st way is to use props

<Row id = "someRandomID">

Wherein, in the Definition, you may just go

const Row = props  => {
 div id = {props.id}
}

The same could be done with class, replacing id with className in the above example.


You might as well use react-html-id, that is an npm package. This is an npm package that allows you to use unique html IDs for components without any dependencies on other libraries.

Ref: react-html-id


Peace.



来源:https://stackoverflow.com/questions/38486660/how-to-add-a-classname-id-to-react-bootstrap-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!