react在jsx語法中實現for迴圈

主機筆記發表於2016-12-14
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="react.js"></script>
    <script src="react-dom.js"></script>
    <script src="babel.min.js"></script>
<style>
    .yew{color:red;}
    .no{color:#000;}
</style>
</head>
<body>
    <div id="root"></div>
<script type="text/babel">
    class Hello extends React.Component {
        render(){
        var list = (length) => {
          var res = [];
          for(var i = 0; i < length; i++) {
            res.push(<h2 key={i}>hello react</h2>)
          }
          return res
        }
        return (
          <div>hello {list(this.props.length)}
          </div>
        )
      }
    }
var helloData = 5
    ReactDOM.render(
      <Hello length = {helloData}/>,
      document.getElementById(`root`)
    );

</script>
</body>
</html>

 


相關文章