React
官网:React (docschina.org)
安装React
html中直接导入
1 2 3
| <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script> <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script> <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
|
- react.min.js - React 的核心库
- react-dom.min.js - 提供与 DOM 相关的功能
- babel.min.js - Babel 可以将 ES6 代码转为 ES5 代码,这样我们就能在目前不支持 ES6 浏览器上执行 React 代码。Babel 内嵌了对 JSX 的支持。通过将 Babel 和 babel-sublime 包(package)一同使用可以让源码的语法渲染上升到一个全新的水平。
使用create-react-app构建项目
create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境
官网:Create React App (create-react-app.dev)
1 2 3
| npx create-react-app my-app cd my-app npm start
|
概念
JSX
JavaScript + XML,js的语法扩展,可以在JavaScript中写XML
使用create-react-app创建的项目中有babel,经过babel编译处理后才能在浏览器环境中使用
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import React from 'react' import ReactDOM from 'react-dom'
const element = ( <h1>Hello, JSX</h1> );
ReactDOM.render( element, document.getElementById('root') );
const root = ReactDOM.createRoot(document.getElementById('root')); root.render( element );
|
注意点:
- React元素的属性名使用驼峰命名法
- 特殊属性名要转换成对应写法:class -> className、for -> htmlFor、tabindex -> tablndex
- 没有子节点的React元素可以用
/>
结束
- 推荐使用
()
包裹JSX
嵌入JS表达式
语法:{JS表达式}
1 2 3 4
| const name = 'Jack' const dv = ( <div>我叫: {name}</div> )
|
注意点
- 单大括号里可以直接使用js表达式
- JSX自身也是JS表达式
- 不能在{}中出现语句(如:if/for)
条件渲染
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const isLoading = false const loadData = () => { if (isLoading) { return <div>loading...</div> } return <div>数据加载完成</div> }
const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <h1> {loadData()} </h1> );
|
列表渲染
1 2 3 4 5 6 7 8 9 10 11 12
| const songs = [ {id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}, ]
const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <h1> {songs.map(item => <li key={item.id}>{item.name}</li>)} </h1> );
|
样式处理
行内样式
1 2 3 4 5 6 7
| // 第一个{}表示嵌入表达式,第二个{}表示对象 const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <h1 style = {{ color: 'red', backgroundColor: 'skyblue' }}> 样式处理 </h1> );
|
类名
记得导入
1 2 3 4 5 6 7 8 9
| // App.js import './index.css';
const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <h1 className='title'> 样式处理 </h1> );
|
1 2 3 4 5
| // index.css
.title { text-align: center; }
|
组件
props
是父组件传给子组件的属性(只读,单向)
定义组件方式1:函数式
1 2 3
| function MyComp(props) { return <h1>hello {props.name}</h1>; }
|
定义组件方式2:类组件式,需要继承React.Component,需要定义render方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import React;
class MyComp extends React.Component { constructor(props) { super(props); } render() { return <h1>hello {this.props.name}</h1>; } }
|
类组件生命周期

挂载阶段
1 2 3 4 5 6 7
| constructor()
componentWillMount()
render()
componentDidMount()
|
更新阶段
1 2 3 4 5 6 7 8 9
| componentWillReceiveProps(nextProps)
shouldComponentUpdate(nextProps, nextState)
componentWillUpdate(nextProps, nextState)
render()
componentDidUpdate(prevProps, prevState)
|
卸载阶段
1
| componentWillUnmount() { }
|
state&props
props:是父组件传给子组件的属性(只读,单向)
1 2 3 4 5 6 7 8 9
| const HelloWorld = (props) => <div>Hello {props.name}</div>
function App() { return ( <div> <HelloWorld name="Someone :)"/> </div> ); }
|
state:组件自己的数据
1 2 3 4 5 6 7 8 9 10 11
| class MyComp extends React.Component { constructor(props) { super(props); this.state = { time: new Date(), }; } render() { return <h1>Its {this.state.time}</h1>; } }
|
Hello World
创建项目
1
| npx create-react-app my-app
|
精简项目
将public目录里除了favicon.ico
和index.html
的文件删掉
将src里除了App.js
的文件删掉,创建index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import React from 'react'
function App() {
return ( <div className="App"> <h1>Hello World</h1> </div> ); }
export default App;
|
1 2 3 4 5 6
| import React from "react"; import ReactDOM from "react-dom" import App from "./App"
ReactDOM.render(<App/>, document.getElementById('root'))
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| // index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <link rel="icon" href="%PUBLIC_URL%/favicon.ico"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>React App</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="root"></div> </body> </html>
|
