进入 React官网
尚硅谷 React教学视频:b站 | 百度网盘 提取码:c1ac(包含视频、源码、课件)
1. hello react
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hello_react</title> </head> <body> <div id="test"></div>
<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>
<script type="text/babel" > const VDOM = <h1>Hello,React</h1> ReactDOM.render(VDOM,document.getElementById('test')) </script> </body> </html>
|
2. JSX
jsx语法规则:
1.定义虚拟DOM时,不要写引号。
2.标签中混入JS表达式时要用{}。
3.样式的类名指定不要用class,要用className。
4.内联样式,要用的⬇️形式去写。
style={{key:value}}
5.只有一个根标签
6.标签必须闭合
7.标签首字母
(1).若小写字母开头,则将该标签转为html中同名元素,若html中无该标签对应的同名元素,则报错。
(2).若大写字母开头,react就去渲染对应的组件,若组件没有定义,则报错。
1 2 3 4 5
| const VDOM = ( <h1 id="title"> <span>Hello,React</span> </h1> )
|
3. 组件定义方式
函数式组件
1 2 3 4 5 6 7 8 9 10 11 12
| function MyComponent(){ console.log(this); return <h2>我是用函数定义的组件(适用于【简单组件】的定义)</h2> }
ReactDOM.render(<MyComponent/>,document.getElementById('test'))
|
类式组件
- 必须继承React.Component。
- 实现render方法,并返回需要展示的组件内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class MyComponent extends React.Component { render(){ console.log('render中的this:',this); return <h2>我是用类定义的组件(适用于【复杂组件】的定义)</h2> } }
ReactDOM.render(<MyComponent/>,document.getElementById('test'))
|
4. 组件实例三大属性
4.1. props
通过组件的形式传递的参数都会被放到props。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Person extends React.Component{ render(){ const {name,age,sex} = this.props return ( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age+1}</li> </ul> ) } }
ReactDOM.render(<Person name="jerry" age={19} sex="男"/>,document.getElementById('test1'))
|
对传入的参数进行限制:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class Person extends React.Component{ render(){ const {name,age,sex} = this.props return ( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age+1}</li> </ul> ) } }
Person.propTypes = { name:PropTypes.string.isRequired, sex:PropTypes.string, age:PropTypes.number, speak:PropTypes.func, }
Person.defaultProps = { sex:'男', age:18 }
ReactDOM.render(<Person name={100} speak={speak}/>,document.getElementById('test1'))
|
限制简化(将限制处理放入类):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| class Person extends React.Component{
constructor(props){ super(props) console.log('constructor',this.props); }
static propTypes = { name:PropTypes.string.isRequired, sex:PropTypes.string, age:PropTypes.number, }
static defaultProps = { sex:'男', age:18 }
render(){ const {name,age,sex} = this.props return ( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age+1}</li> </ul> ) } }
ReactDOM.render(<Person name="jerry"/>,document.getElementById('test1'))
|
函数组件使用props:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| function Person (props){ const {name,age,sex} = props return ( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age}</li> </ul> ) } Person.propTypes = { name:PropTypes.string.isRequired, sex:PropTypes.string, age:PropTypes.number, }
Person.defaultProps = { sex:'男', age:18 }
ReactDOM.render(<Person name="jerry"/>,document.getElementById('test1'))
|
4.2. state
state: 状态,用于存储组件内数据信息。
constructor: 构造函数,作用是初始化state和绑定事件。
1 2 3 4 5 6 7 8 9
| class Weather extends React.Component{ constructor(props){ console.log('constructor'); super(props) this.state = {isHot:false,wind:'微风'} } }
|
1 2 3 4
| class Weather extends React.Component{ this.state = {isHot:false,wind:'微风'} }
|
4.3. refs
官方不建议过度使用ref。
字符串形式的ref
官方不建议使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Demo extends React.Component{ showData = ()=>{ const {input1} = this.refs alert(input1.value) } showData2 = ()=>{ const {input2} = this.refs alert(input2.value) } render(){ return( <div> <input ref="input1" type="text" placeholder="点击按钮提示数据"/> <button onClick={this.showData}>点我提示左侧的数据</button> <input ref="input2" onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> </div> ) } }
ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test'))
|
回调函数形式的ref
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Demo extends React.Component{ showData = ()=>{ const {input1} = this alert(input1.value) } showData2 = ()=>{ const {input2} = this alert(input2.value) } render(){ return( <div> <input ref={c => this.input1 = c } type="text" placeholder="点击按钮提示数据"/> <button onClick={this.showData}>点我提示左侧的数据</button> <input onBlur={this.showData2} ref={c => this.input2 = c } type="text" placeholder="失去焦点提示数据"/> </div> ) } }
ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test'))
|
createRef 的方式构建ref(React提供的方法)
官方推荐使用的方式。
缺陷:React.createRef() 一次只能创建一个ref
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Demo extends React.Component{
myRef = React.createRef() myRef2 = React.createRef() showData = ()=>{ alert(this.myRef.current.value); } showData2 = ()=>{ alert(this.myRef2.current.value); } render(){ return( <div> <input ref={this.myRef} type="text" placeholder="点击按钮提示数据"/> <button onClick={this.showData}>点我提示左侧的数据</button> <input onBlur={this.showData2} ref={this.myRef2} type="text" placeholder="失去焦点提示数据"/> </div> ) } }
ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test'))
|
5. 生命周期
React 新生命周期(React v16之后):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| class Count extends React.Component{
constructor(props){ console.log('Count---constructor'); super(props) this.state = {count:0} }
add = ()=>{ const {count} = this.state this.setState({count:count+1}) }
death = ()=>{ ReactDOM.unmountComponentAtNode(document.getElementById('test')) }
force = ()=>{ this.forceUpdate() }
static getDerivedStateFromProps(props,state){ console.log('getDerivedStateFromProps',props,state); return null }
getSnapshotBeforeUpdate(){ console.log('getSnapshotBeforeUpdate'); return 'atguigu' }
componentDidMount(){ console.log('Count---componentDidMount'); }
componentWillUnmount(){ console.log('Count---componentWillUnmount'); }
shouldComponentUpdate(){ console.log('Count---shouldComponentUpdate'); return true }
componentDidUpdate(preProps,preState,snapshotValue){ console.log('Count---componentDidUpdate',preProps,preState,snapshotValue); }
render(){ console.log('Count---render'); const {count} = this.state return( <div> <h2>当前求和为:{count}</h2> <button onClick={this.add}>点我+1</button> <button onClick={this.death}>卸载组件</button> <button onClick={this.force}>不更改任何状态中的数据,强制更新一下</button> </div> ) } }
ReactDOM.render(<Count count={199}/>,document.getElementById('test'))
|
React v16 前生命周期:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
class Count extends React.Component{
constructor(props){ console.log('Count---constructor'); super(props) this.state = {count:0} }
add = ()=>{ const {count} = this.state this.setState({count:count+1}) }
death = ()=>{ ReactDOM.unmountComponentAtNode(document.getElementById('test')) }
force = ()=>{ this.forceUpdate() }
componentWillMount(){ console.log('Count---componentWillMount'); }
componentDidMount(){ console.log('Count---componentDidMount'); }
componentWillUnmount(){ console.log('Count---componentWillUnmount'); }
shouldComponentUpdate(){ console.log('Count---shouldComponentUpdate'); return true }
componentWillUpdate(){ console.log('Count---componentWillUpdate'); }
componentDidUpdate(){ console.log('Count---componentDidUpdate'); }
render(){ console.log('Count---render'); const {count} = this.state return( <div> <h2>当前求和为:{count}</h2> <button onClick={this.add}>点我+1</button> <button onClick={this.death}>卸载组件</button> <button onClick={this.force}>不更改任何状态中的数据,强制更新一下</button> </div> ) } }
class A extends React.Component{ state = {carName:'奔驰'}
changeCar = ()=>{ this.setState({carName:'奥拓'}) }
render(){ return( <div> <div>我是A组件</div> <button onClick={this.changeCar}>换车</button> <B carName={this.state.carName}/> </div> ) } }
class B extends React.Component{ componentWillReceiveProps(props){ console.log('B---componentWillReceiveProps',props); }
shouldComponentUpdate(){ console.log('B---shouldComponentUpdate'); return true } componentWillUpdate(){ console.log('B---componentWillUpdate'); }
componentDidUpdate(){ console.log('B---componentDidUpdate'); }
render(){ console.log('B---render'); return( <div>我是B组件,接收到的车是:{this.props.carName}</div> ) } }
ReactDOM.render(<Count/>,document.getElementById('test'))
|
6. 相比于HTML的语法区别
HTML的class - > React 的 className
HTML的style=”” - > React 的 style={{}}
参考资料