Posted in
4992
11:29 pm, October 14, 2021
How to Compose React Components in React
In this code we nest the components NonCitrus
and Citrus
into the Fruits Component.
Then nest the Fruits within the TypesOfFood
component.
Also added the additional components, Vegetables
, NonCitrus
and Citrus
so that the code is complete.
HTML
<div id="root"></div>
Scripts
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.0.0-alpha-5fa4d79b0-20211008/umd/react.production.min.js" integrity="sha512-5PVmWGoNJocWPdQJmJd1aRbz3cFcFgXctWKLWcitqtgX64jF+ttfg9g2oLltmeQ1HUo3gT6QchaMK3h+S+JG4Q==" crossorigin="anonymous"
referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.0.0-alpha-5fa4d79b0-20211008/umd/react-dom.production.min.js" integrity="sha512-pUsjUv+9XgkTn+UbLyNIT4YNZPF2p0E45FBKmDL7Ti8iovYwp2CUkQs6Q7J9y5scLxWaOM+T5jJc0ls+WHUcmQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class Citrus extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h2>Citrus:</h2>
</div>
);
}
};
class NonCitrus extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h2>Non Citrus:</h2>
</div>
);
}
};
class Vegetables extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h2>Vegetables:</h2>
</div>
);
}
};
class Fruits extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h2>Fruits:</h2>
{ /* Change code below this line */ }
<NonCitrus />
<Citrus />
{ /* Change code above this line */ }
</div>
);
}
};
class TypesOfFood extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h1>Types of Food:</h1>
{ /* Change code below this line */ }
<Fruits />
{ /* Change code above this line */ }
<Vegetables />
</div>
);
}
};
//ReactDOM.render(MyComponent, document.getElementById('root'))
ReactDOM.render(<TypesOfFood />, document.getElementById('root'))
</script>
External Link for How to Compose React Components in React
View Statistics
This Week
120
This Month
677
This Year
1406
Add Comment
Other Items in react
React - Use State to Toggle an Element
Bind 'this' to a Class Method
React - Create a Stateful Component
Using props with Stateless Functional Components in React
How to Access Props Using this.props in React
How to Use PropTypes to Define Props in React
How to Override Default Props in React
How to use default props in React
How to Pass an Array as Props in React
How to Pass Props to a Stateless Functional Component in React
How to Write a React Component from Scratch
How to Render a React Class Component to the DOM
How to Compose React Components in React
How to Use React to Render Nested Components
How to Create a Component with Composition in React
How to Create a React Component using the ES6 class method
What is a Stateless Functional Component in React
How do Self-Closing JSX Tags Work?
How to Define a HTML Class in JSX in React
How to Render HTML Elements to the DOM with React
How to Add Comments in JSX
Create a Complex JSX Element
Create a Simple JSX Element
adding react with JSX using babel
adding react to a website in 3 steps
using babel for react compiling in browser
react hello world with time and date updating
react hello world with variable name
react basic hello world
Related Search Terms
Other Categories in Code
alpine js apps c css factorio font awesome images linux quick modals sqlite site bugs site updates slick slider sliders testing windows apps apache api apps asp bat bootstrap bootstrap templates charts cookies core css css filters css grid design elements docker domains emoji fancybox fonts foundation framework gimp git html icons ideas image formatting images javascript javascript functions jquery js linux mac misc modals mysql nginx node php php errors php function php functions php simple html dom pi400 python react regex sections simple_html_dom simplepie php site bugs site documentation slick slider sql sqlite ssh sublime svg svg css templates tools virtual box vscode vue webdev windows windows 11 windows commands wordpress