核心库
下载
官网 点击‘GitHub’–>整体包的下载
历史版本
浏览器开发下载如下:
<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 src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
也可以使用python脚本先获取三个js库
import requests
url = "https://unpkg.com/react@16.14.0/umd/react.development.js"
url1 = "https://unpkg.com/react-dom@16/umd/react-dom.development.js"
url2 = "https://unpkg.com/babel-standalone@6.15.0/babel.min.js"
urls = [url,url1,url2]
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"}
for i in urls:
res = requests.get(i,headers=headers)
filename = i.split("/")[-1]
with open(filename,"wb") as f:
f.write(res.content)
导入核心库
<script type="text/javascript" src="/js/react.development.js">
<script type="text/javascript" src="/js/react-dom.development.js">
<script type="text/javascript" src="js/babel.min.js">
以上三个核心库,需要按照顺序导入
使用案例
创建一个文件夹 reactProject>js目录 reactProject>pages目录 pages>helloReact.html
hellpReact.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>react project</title>
</head>
<body>
<script type="text/javascript" src="../js/react.development.js"></script>
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript" src="../js/babel.min.js"></script>
<div id="content"></div>
<script type="text/babel">
const VDOM = <div>hello React,this is my first reactProject!</div>
ReactDOM.render(VDOM,document.getElementById("content"))
</script>
</body>
</html>
在vscode运行页面 右键–>open with Live Server 没有则需要安装live server插件
以上代码需在开发环境中运行,生成环境还需编译
使用js创建虚拟DOM
<script type="text/javascript">
const VDOM = React.createElement("div",{id:"block1"},"content")
const VDOM1 = React.createElement("div",{id:"block2"},React.createElement("div",{id:"block3"},React.createElement("div",{id:"block4"},"多层嵌套")))
console.log(VDOM)
console.log(document.getElementById("content"))
console.log(typeof VDOM)
</script>
当多层次DOM节点嵌套时,使用js创建DOM节点相当繁琐,所以使用jsx创建虚拟DOM,简化
jsx
javascript + xml 是react定义的类似xml的js扩展语法 本质:React.createElement(component,props,content) 作用:简化创建虚拟DOM
const VDOM = <div id="content">this is content container</div>
- 不是字符串,不同于js
- 标签不是html/xml,但可以转为html
小写的标签,会自动转为html对应的标签,但是如果html不存在对应的标签,报错 大写首字母,自动渲染React组件,若组件没有定义则报错 - 涉及js变量时,使用{ myId },类名属性使用className=“bg”
- 内联的style = {{color:“red”,}},内部只能有一个根节点,且每个节点必须闭合
- 示例
<script type="text/babel">
var myId = 20
const VDOM = (
<div>
<h1 className="title" id={myId}>
<span style={{color:"white"}}>{myData}</span>
</h1>
<input type="text" />
<hello>hello</hello>
<Hello>Component</Hello>
</div>
)
</script>
案例:动态显示数据列表
jack-18 tom-20 lucy-40 … ajax向后端发送请求,拿到数据,处理数据,创建虚拟DOM,呈现页面
js表达式与语句
- 表达式,有返回值
如:a,a+b , arr.map(e=>{return e+1}),func(),function test(){}… - 语句,没有返回值
if(){} for(){} switch(){}
VDOM中只能写 {js表达式},给一个数组,可以遍历,但是对象不行。
实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic data</title>
</head>
<body>
<script type="text/javascript" src="../js/react.development.js"></script>
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript" src="../js/babel.min.js"></script>
<ul id="list"></ul>
<script type="text/babel">
let arr = ["jack","tom","lucy"]
const VDOM = (
arr.map((element,index)=>{
return <li key={index}>{element}</li>
})
)
ReactDOM.render(VDOM,document.getElementById("list"))
</script>
</body>
</html>
? ? ? ? 上一篇:js框架----react.js????下一篇:
|