If you want to pass some nested HTML elements or components to be rendered in a specific place by a parent
component in react.js and create some reusable components that define the surrounding structure like
defining a generic dialog or modal component to wrap arbitrary elements in, in this quick tips episode, you
will learn how. Above is the vid and below you will find some useful notes.
- 1.
Pre-reqs
-
Have node.js installed
-
- 2.
Rendering a Nested Component in React.js
-
Create the app using npx create-react-app
npx create-react-app quick-tips-react-children
-
Start the app
npm start
-
Create the parent component using the children property
import React from 'react'; const ParentComponent = (props) => { return ( <div> <p>Parent component</p> {props.children} <p>After children content</p> </div> ); }; export default ParentComponent;
-
Use the parent component and nest some components inside it
<ParentComponent> <a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer"> Learn React </a> </ParentComponent>
-
- 3.
Comentarios
Publicar un comentario