1

I was playing around with react-bootstrap components and saw that their modals are structured like this:

<Modal>
  <Modal.Header>
    ...
  </Modal.Header>
  <Modal.Body>
    ...
  </Modal.Body>
</Modal>

I'm just curious, how are they able to create a react component that also holds sub-components such as <Modal.Header>? I'm making a lot of custom components in my project and I would love to structure a few of my components like this.

1
  • The beauty of JS is that everything is open source. You can read their code. If you are using it in a repo, you can navigate to the folder in node_modules. If not, you can check debug version as well: cdnjs.cloudflare.com/ajax/libs/react-bootstrap/2.5.0/… Commented Sep 23, 2022 at 7:29

1 Answer 1

2

Like this:

const Modal = () => <div>Modal</div>

Modal.Body = () => <div>Body</div>

export default Modal

And now you can use <Modal.Body />

Sign up to request clarification or add additional context in comments.

3 Comments

Is this considered bad practice? It seems sort of odd storing the sub-components in the function object itself
If you dont abuse it and it does make sense that's not a bad practice. Functions in Js are objects like any other, so feel free to use that as your advantage.
Well, thank you then for the insight! This has helped a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.