When using react-css-modules in a project, I found myself trying to do something that can be simplified to this:
import React from 'react'
import CSSModules from 'react-css-modules'
import ChildComponent from './ChildComponent.jsx'
import styles from './ParentComponent.css'
class ParentComponent extends React.Component {
render() {
return (
<div>
<p styleName='parentComponent'>This is rendered in ParentComponent</p>
<ChildComponent styleName='some-name' /> // this style is not applied
</div>
)
}
}
export default CSSModules(ParentComponent, styles)
The CSS class some-name
is defined in ParentComponent.css
and contains rules that will position ChildComponent
within ParentComponent
.
ChildComponent
implementation looks like this:
import React from 'react'
import CSSModules from 'react-css-modules'
import styles from './ChildComponent.css'
class ChildComponent extends React.Component {
render() {
return (
<div> // expected to have this div decorated with 'some-name' class
<p styleName='childComponent'>This is rendered in ChildComponent</p>
<button styleName='childComponent'>This is rendered in ChildComponent</button>
</div>
)
}
}
export default CSSModules(ChildComponent, styles)
I was expecting to receive the CSS defined in the some-name
class in the first element of what's rendered on ChildComponent
(the wrapping ), but this is not happening and 'some-class' is just ignored.
I could workaround this issue by adding an extra wrapping and applying styles to it, but I would like to avoid this if possible:
// in ParentComponent do this instead
render() {
return (
<div>
<p styleName='parentComponent'>This is rendered in ParentComponent</p>
<div styleName='some-name'>
<ChildComponent />
</div>
</div>
)
}
Hopefully I have explained the use case and the issue well enough, so any recommendation on how to do this will be much appreciated.