Blog>
Snippets

Passing React Elements as Props

Exemplify how a parent component can pass entire react elements or components to a child component as props.
function WelcomeMessage({ children }) {
  return <div>{children}</div>;
}
This is a child component that expects React elements to be passed through its `children` prop, which it then renders inside a div.
function ParentComponent() {
  const message = <p>Hello, World!</p>;
  return <WelcomeMessage>{message}</WelcomeMessage>;
}
Here, the ParentComponent is creating a React element (message) and passing it to the child component (WelcomeMessage) by including it between the opening and closing tags of WelcomeMessage.