React Getting Started-creating components (2) Non-state functional Function Method __ function

Source: Internet
Author: User


Previously, the react component was created by inheriting React.component. The
can also construct react components through stateless functional functions. 

Example


<! DOCTYPE html>
<html>
<head>
    <meta charset = "utf-8">
    <title> React Components </ title>
</ head>
<body>

<!-Target Container->
<div id = "react-container"> </ div>

<!-React Library & 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>
    const foodList = ((items)) =>
        React.createElement (
              "ul",
              {className: "food-list"},
             items.map ((item, i) =>
                React.createElement ("li", {key: i}, item)
             )
     )

    const items = [
        "1 apple",
        "1 banana",
        "2 oranges",
        "2 tomatos"
    ]
    ReactDOM.render (
      React.createElement (foodList, {items}, null),
      document.getElementById ('react-container')
    )
</ script>

</ body>
</ html>
Declare a stateless function foodList:
items is the parameter parameter passed in;
React.createElement is the function body and return value

const foodList = ((items)) =>
        React.createElement (
              "ul",
              {className: "food-list"},
             items.map ((item, i) =>
                React.createElement ("li", {key: i}, item)
        )
 )
Another anonymous function is the function passed as a parameter in items.map ()
item is each value in the items array, i is the index of the items array, item = items [i]

items.map ((item, i) =>
       React.createElement ("li", {key: i}, item)
)
The role of this sentence is to convert each value in items into a React Element.
Each item is structured as a <li> corresponding to the html, containing a key attribute and the content of the item.

ReactDOM.render (
      React.createElement (foodList, {items}, null),
      document.getElementById ('react-container')
)
When ReactDom renders, construct React.createElement with the stateless function foodList created earlier, and pass in the actual parameter {items}.

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.