当前位置:   article > 正文

react项目结构以及jsx模板语法_react模板语法

react模板语法

1.创建一个react项目

create-react-app 快速脚手架

npm install -g create-react-app

创建React项目

create-react-app xxx start

 创建成功!

2.了解项目目录结构

node_modules — 项目依赖包文件夹

public — 静态资源文件夹

  • favicon.icon — 网站偏爱图标
  • index.html — 主页面(重要)
  • logo192.png — logo图
  • logo512 — logo图
  • manifest.json — 应用加壳的配置文件
  • robots.txt — 爬虫协议文件

src — 源码文件夹

  • App.css — App组件的样式
  • App.js — App组件 (重要)
  • App.test.js — 给App测试
  • index.css — 样式
  • index.js — 入口文件 (重要)
  • logo.svg — logo图
  • reportWebVitals.js — 页面性能分析文件(需要web-vitals库的支持)
  • setupTests.js — 组件单元测试的文件(需要jest-dom库的支持)

gitignore — git的选择性上传的配置文件

配置不会上传的文件信息。

开始学习

jsx语法结构

  • ​  1.jsx模板语法中双标签必须要有开始和结束
  • ​  2.jsx模板语法中单标签必须要有结束
  • ​  3.jsx模板语法中有且只能有一个根标签
  • ​  4.jsx模板语法严格区分大小写

 jsx模板语法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <!-- 2.引入react -->
  9. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  10. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  11. <!-- 3.引入babel -->
  12. <script src="./libs//babel.min.js"></script>
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <script type="text/babel">
  17. const div = <div>
  18. <h3>111</h3>
  19. <p>
  20. <h2>222</h2>
  21. </p>
  22. </div>
  23. ReactDOM.render(div,document.querySelector('#container'))
  24. </script>
  25. </body>
  26. </html>

模板变量的使用

 文本内容:使用{}解析js语法
属性: 属性名={js语法}

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <!-- 2.引入react -->
  9. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  10. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  11. <!-- 3.引入babel -->
  12. <script src="./libs//babel.min.js"></script>
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <script type="text/babel">
  17. const msg = '111';
  18. const div = <div>
  19. <h3>{msg}</h3>
  20. </div>
  21. ReactDOM.render(div,document.querySelector('#container'))
  22. </script>
  23. </body>
  24. </html>

模板方法的使用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <!-- 2.引入react -->
  9. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  10. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  11. <!-- 3.引入babel -->
  12. <script src="./libs//babel.min.js"></script>
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <script type="text/babel">
  17. const str = 'i have an apple';
  18. const div = <div>
  19. <div>{str.toUpperCase()}</div>
  20. </div>
  21. ReactDOM.render(div,document.querySelector('#container'))
  22. </script>
  23. </body>
  24. </html>

调用自定义方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <!-- 2.引入react -->
  9. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  10. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  11. <!-- 3.引入babel -->
  12. <script src="./libs//babel.min.js"></script>
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <script type="text/babel">
  17. const str = 'i have an apple';
  18. function fn(str){
  19. return str.split('').reverse().join('')
  20. }
  21. const div = <div>
  22. <div>{str.toUpperCase()}</div>
  23. <div>{fn(str)}</div>
  24. </div>
  25. ReactDOM.render(div,document.querySelector('#container'))
  26. </script>
  27. </body>
  28. </html>

模板注释

{/*这里写注释内容*/}


条件渲染

  • 不能使用if结构做条件判断
  • 使用三元运算符做条件判断
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <!-- 2.引入react -->
  9. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  10. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  11. <!-- 3.引入babel -->
  12. <script src="./libs//babel.min.js"></script>
  13. </head>
  14. <body>
  15. <div id="container">
  16. </div>
  17. <script type="text/babel">
  18. let age = 30;
  19. const div = <div>
  20. {/*<div>{if(age>=18){'已成年'}else{'未成年'}}</div>*/}
  21. <div>{age>=18 ? '已成年' : '未成年'}</div>
  22. <hr />
  23. <div>{age>=18 ?<h2>已成年</h2> : <h3>未成年</h3>}</div>
  24. </div>
  25. ReactDOM.render(div,document.querySelector('#container'))
  26. </script>
  27. </body>
  28. </html>

关键字

  • className 为设置标签的选择器
  • htmlFor 设置label标签的属性
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <!-- 2.引入react -->
  9. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  10. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  11. <!-- 3.引入babel -->
  12. <script src="./libs//babel.min.js"></script>
  13. <style>
  14. .main{
  15. background: skyblue;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="container">
  21. </div>
  22. <script type="text/babel">
  23. const div = <div className="main">
  24. <h3>22</h3>
  25. <label htmlFor="search">搜索</label>
  26. <input type="text" id="search" />
  27. </div>
  28. ReactDOM.render(div,document.querySelector('#container'))
  29. </script>
  30. </body>
  31. </html>
  • 通过style指定行内样式

  • style属性值采用{{}}来书写

{{}}:
外层{}:  #指jsx模板语法
内层:{}:    #指一个json对象
 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <!-- 2.引入react -->
  9. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  10. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  11. <!-- 3.引入babel -->
  12. <script src="./libs//babel.min.js"></script>
  13. <style>
  14. .main{
  15. background: skyblue;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="container">
  21. </div>
  22. <script type="text/babel">
  23. const obj = {background:'red',color:'yellow'};
  24. // 书写jsx模板语法
  25. const div = <div className="main">
  26. {/*错误写法*/}
  27. {/*<h3 style="background:'red';color:'yellow'">啊哈哈哈哈哈哈</h3>*/}
  28. <h3 style={obj}>咿呀呀呀呀呀</h3>
  29. </div>
  30. ReactDOM.render(div,document.querySelector('#container'))
  31. </script>
  32. </body>
  33. </html>

列表的渲染

  • 在遍历数组时,key需要绑定在遍历的直接子元素上
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. <!-- 2.引入react -->
    9. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    10. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    11. <!-- 3.引入babel -->
    12. <script src="./libs//babel.min.js"></script>
    13. <style>
    14. .main{
    15. background: skyblue;
    16. }
    17. </style>
    18. </head>
    19. <body>
    20. <div id="container">
    21. </div>
    22. <script type="text/babel">
    23. let arr = ['朝阳区','海淀区','通州区'];
    24. let arr1 = [<h2 key="0">海淀区</h2>,<h2 key="1">朝阳区</h2>,<h2 key="2">通州区</h2>]
    25. // forEach 没有返回值
    26. let arr2 = [];
    27. arr.forEach(item=>{
    28. arr2.push(item)
    29. })
    30. // map #有返回值
    31. let arr3 = arr.map((item,index)=>{
    32. return <h3 key={index}>{item}</h3>
    33. })
    34. let brand = [
    35. {
    36. id:1,
    37. name:'宝马',
    38. price:500000
    39. },
    40. {
    41. id:2,
    42. name:'奔驰',
    43. price:1000000
    44. },
    45. {
    46. id:3,
    47. name:'奥迪',
    48. price:400000,
    49. }
    50. ]
    51. // 书写jsx模板语法
    52. const div = <div className="main">
    53. {arr}
    54. <hr />
    55. {arr1}
    56. <hr />
    57. {arr2}
    58. <hr />
    59. {arr3}
    60. <hr />
    61. {arr.map((item,index)=>{
    62. return <h3 key={index}>{item}</h3>
    63. })}
    64. {/*变形写法*/}
    65. {arr.map((item,index)=><h3 key={index}>{item}</h3>)}
    66. <hr />
    67. {/*在遍历数组时,key需要绑定在遍历的直接子元素上*/}
    68. {brand.map(item=>(
    69. <div key={item.id}>
    70. <h2>{item.name}</h2>
    71. <div>{item.price}</div>
    72. </div>
    73. ))}
    74. </div>
    75. ReactDOM.render(div,document.querySelector('#container'))
    76. </script>
    77. </body>
    78. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/138978
推荐阅读
相关标签
  

闽ICP备14008679号