React Getting Started-creating components (1) Inheriting component method

Source: Internet
Author: User


The React component (Component) is also an element, but it is more granular and contains more child elements.



Through the react component, some related elements are organized to form a reusable combination of elements with multiple members. examples


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React Components</title>
</head>
<body>
<!-- Target Container -->
<div id="react-container"></div>
<!-- React Library &amp; React DOM-->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script>
class foodList extends React.Component {
Render () {
return React.createElement("ul", {"className": "food-list"},
React.createElement("li", null, "1 apple"),
React.createElement("li", null, "1 banana"),
React.createElement("li", null, "2 oranges"),
React.createElement("li", null, "2 tomatos")
)
}
}
const list = React.createElement(foodList, null, null)
ReactDOM.render(
List,
document.getElementById('react-container')
)
</script>
</body>
</html>
1. Create components by inheriting react.component
class foodList extends React.Component {
}
2. The render function must be included in the component
class foodList extends React.Component {
Render () {
//Code to create the element
}
}
3. Returns a combination of multiple children of an element in render
Render () {
return React.createElement(
"UL",
{"className": "food-list"},
React.createElement("li", null, "1 apple"),
React.createElement("li", null, "1 banana"),
React.createElement("li", null, "2 oranges"),
React.createElement("li", null, "2 tomatos")
)
}
4. Create the list element by passing in the component foodlist as a parameter through createElement
const list = React.createElement(foodList, null, null)
5, rendering
ReactDOM.render(
List,
document.getElementById('react-container')
)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.