-1

I saw that some libraries like Ant Design have components that look like this:

<Form.Item></Form.Item>

As we can see, the component above is created like a object, using . -> Form.Item.

Can someone give a simple example of how to create components like this? I don't understand how they are created. I understand when I create a <Nav/> component, but not how to create a component what will be extended with a ., like <Nav.Home/> or <Nav.Common/>. Thanks.

1

1 Answer 1

3

A React <JSX /> component just compiles to plain JavaScript. So you can create them as properties of an object just like you could with any other JavaScript value.

const Components = {
  Friend: ({name}) => <div>I am your friend {name}</div>,
}

export default function App() {
  return (
    <div className="App">
      <Components.Friend name="Jimmy" />
    </div>
  )
}
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.