Wrapper Components

Using Wrappers to handle UX/style variations

For Handling Wrapper <div>’s and other markup around component, use composition!

When you create a React component instance, you can include additional React components or JavaScript expressions between the opening and closing tags.
Parent can read its children by accessing the special this.props.children prop.

  1. const SampleComponent = () => {
  2. <Parent>
  3. <Child />
  4. </Parent>
  5. };
  6. const Parent = () => {
  7. // You can use class 'bla' or any other classes to handle any style variations for the same markup.
  8. <div className="bla">
  9. {this.props.children}
  10. </div>
  11. };

FYI - Wrapper component can also be made accept a tag name and then used to generate the HTML element.
However, usually this is not recommended because you can’t add attributes/props to it.

  1. const SampleComponent = () => {
  2. <Wrap tagName="div" content="Hello World" />
  3. };
  4. const Wrap = ({ tagName, content }) => {
  5. const Tag = `${tagName}` // variable name must begin with capital letters
  6. return <Tag>{content}</Tag>
  7. }