1

I would like to create a Dropdown component : <Dropdown />.

In this Dropdown, I would like a handler and a content to show. Always 2 elements inside this Dropdown.

This handler can be anything: Simple HTML, String or Component.

This content to show can be anything: Simple HTML, String or Component.

How can I a structure this Dropdown ? I would like to reuse it anywhere in my app.

I have this in mind.

<div id="Page2">
  <Dropdown>
    <Avatar />
    <Menu>
       <li>Settings</li>
       <li>Logout</li>
    </Menu>
  </Dropdown>
</div>

But it could be this :

<div id="Page3">
  <Dropdown>
    <span>Click to show the content</span>
    <div>Hello World</div>
  </Dropdown>
</div>

Component :

class Dropdown extends React.Component {
   constructor(props) {
       super(props);
   }
}

How can make Avatar or <span>Click to show the content</span> as handler?

I ask because it seems that Dropdown component have dynamic content but the operation is the same.

Thanks

1 Answer 1

1

If I understand you correctly, you are looking for props.children. This is used to pass the children elements of a component to the component.

For example:

<DialogBox>
  <div>Title</div>
</DialogBox>

In the DialogBox component's render method:

return (
  <div class="dialog-box">
    {this.props.children}
  </div>
);

This will render:

<div class="dialog-box">
  <div>Title</div>
</div>

Read more about it here: https://facebook.github.io/react/docs/composition-vs-inheritance.html#containment

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

Comments

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.