16.列表渲染.html 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./babel.min.js"></script>
  8. <script src="./react.development.js"></script>
  9. <script src="./react-dom.development.js"></script>
  10. </head>
  11. <body>
  12. <div id="root"></div>
  13. <!--
  14. React主要使用的是Js方法(map/forEach/for)
  15. -->
  16. <script type="text/babel">
  17. class App extends React.Component {
  18. constructor() {
  19. super();
  20. this.state = {
  21. list: [
  22. {
  23. id:1,
  24. name:'图图',
  25. age:3
  26. },
  27. {
  28. id:2,
  29. name:'小美',
  30. age:4
  31. },
  32. {
  33. id:3,
  34. name:'大壮',
  35. age:5
  36. }
  37. ]
  38. }
  39. this.renderMain = this.renderMain.bind(this)
  40. }
  41. renderMain() {
  42. // console.log("走进来")
  43. // return this.state.list.map((item) => {
  44. // return <p>我叫{item.name}</p>
  45. // })
  46. let newList = [];
  47. // this.state.list.forEach((item) => {
  48. // newList.push(<p>我叫{item.name}</p>)
  49. // console.log(newList,'1')
  50. // })
  51. // console.log(newList)
  52. // return newList;
  53. for(let i=0;i<this.state.list.length;i++) {
  54. newList.push(
  55. <p>我叫{this.state.list[i].name}</p>
  56. )
  57. console.log(newList)
  58. }
  59. return newList;
  60. }
  61. render() {
  62. return (
  63. <div>
  64. <h1>列表渲染</h1>
  65. <p>{this.renderMain()}</p>
  66. </div>
  67. )
  68. }
  69. }
  70. const element = <App/>
  71. const container = document.getElementById("root");
  72. const root = ReactDOM.createRoot(container)
  73. root.render(element);
  74. </script>
  75. </body>
  76. </html>