react基础03--render渲染、组件生命周期、组件通信
1 介绍
react基础02–基础函数&自定义函数组件&属性 中介绍了组件基础函数、自定义函数组件的方法,以及如何设置组件属性。本文在其基础上继续介绍,render渲染、组件生命周期函数、组件通信等常用的方法概念,并配以实际案例。
2 方法&案例
2.1 render 方法
vim index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>React App02</title>
</head>
<body>
<div id="root">
</div>
</body>
</html>
通过函数的方式使用
vim index.js
import React from 'react'; // 只要js中引 入了Reat模块,就可以直接写HTML
import ReactDOM from 'react-dom';
import App from './App'
var myapp = new App();
myapp.i = 2
ReactDOM.render(<div>hello, {myapp.m()}</div>, document.getElementById('root'));
vim App.js
import React from "react";
class App extends React.Component {
constructor(){
super();
console.log('构造方法执行')
this.i = 0;
}
m(){
return (
<h1> this is APP Component, i={this.i}</h1>
)
}
}
export default App
结果:
通过组件的方式使用
vim index.js
import React from 'react'; // 只要js中引 入了Reat模块,就可以直接写HTML
import ReactDOM from 'react-dom';
import App from './App'
ReactDOM.render(<div>hello, <App i={3*3} mesg="word" /></div>
, document.getElementById('root'));
vim App.js
import React from "react";
class App extends React.Component {
constructor(){
super();
console.log('构造方法执行')
}
render(){
return (
<h1> this is APP Component, i={this.props.i},mesg={this.props.mesg}</h1>
)
}
}
export default App
结果:
注意:
App 继承 React.Component, 该组件有 props,因此可以直接使用 props;
props 是 readonly 类型的,不可以对其进行修改。
2.2 state 状态更新
默认不保存状态
vim index.js
import React from 'react'; // 只要js中引 入了Reat模块,就可以直接写HTML
import ReactDOM from 'react-dom';
import App from './App'
ReactDOM.render(<div>hello, <App i={3*3} mesg="word" /></div>
, document.getElementById('root'));
vim App.js
import React from "react";
class App extends React.Component {
constructor(){
super();
this.name = "tom"
}
render(){
return (
<div>
this is App Component: {this.name}
<p>
<button onClick={this.click.bind(this)}>按钮</button>
</p>
</div>
)
}
click(){
this.name = 'lily'
console.log(this.name)
}
}
export default App
结果:
如下图,点击后控制台输出lily, 但是前端页面仍然为 tom,即数据没有被更新
通过state保存状态
vim App.js
import React from "react";
class App extends React.Component {
constructor(){
super();
this.state = {
name: "tom",
age: 10
}
}
render(){
return (
<div>
this is App Component: {this.state.name}
<p>
<button onClick={this.click.bind(this)}>按钮</button>
</p>
</div>
)
}
click(){
// 视图更新
this.setState({name: 'lily'})
console.log(this.state.name)
}
}
export default App
结果:
初次启动的时候 name 为tom,单击 button 后 会保存状态,并更新组件中name为 lily。
初次启动:
单击两次button:
2.3 生命周期函数
react 组件生命周期可以大致分为三类,包括 Mounting(加载阶段)、Updating(更新阶段)和Unmounting(卸载阶段),每个阶段都会工具特定状态执行一系列函数。
以下案例中验证了默认状态下执行的主要函数、组件更新条件下的执行主要函数。
vim App.js
import React from "react";
class App extends React.Component {
constructor(){
super();
this.state = {
name: "tom",
age: 10
}
console.log('constructor...')
}
UNSAFE_componentWillMount(){
console.log('componentWillMount...')
}
componentDidMount(){
console.log('componentDidMount...')
}
UNSAFE_componentWillUpdate(){
console.log('componentWillMount...')
}
componentDidUpdate(){
console.log('componentDidUpdate...')
}
render(){
console.log('render...')
return (
<div>
this is App Component: {this.state.name}
<p>
<button onClick={this.click.bind(this)}>按钮</button>
</p>
</div>
)
}
click(){
// 视图更新,异步函数
this.setState({name: 'lily'}, ()=>{
console.log('setState()回调函数...')
})
console.log('click...')
}
}
export default App
结果:
如下图,正常情况下依次执行 constructor… -》componentWillMount… -》 render… -》 componentDidMount…
当单击按钮后,执行 click… -》componentWillMount… -》 render… -》 componentDidUpdate… -》 setState() ,可见更新视图是异步操作
如果设置 shouldComponentUpdate(){return false} 那么点击click 后将不会更新视图。
Update on Async Rendering 提到 17.0: 将会移除 componentWillMount, componentWillReceiveProps, and componentWillUpdate . (只有新的 “UNSAFE_” 生命周期函数可用)
2.4 组件通信
react 中父子组件可以通过值的方式传参数,父组件通过 props 传参数给子组件,子组件可以通过执行父组件传入的函数来和父组件交互数据。
vim index.js
import React from 'react'; // 只要js中引 入了Reat模块,就可以直接写HTML
import ReactDOM from 'react-dom';
import Demo from './Demo'
ReactDOM.render(<div><Demo/></div>, document.getElementById('root'));
vim App.js
import React from "react";
class App extends React.Component {
constructor(props){
super(props);
this.state = {
name: "tom",
age: 10
}
}
render(){
return (
<div>
<hr/>
this is App Component: {this.state.name}
<br/>
<button onClick={this.click.bind(this)}>App's button</button>
</div>
)
}
click(){
this.props.mesg(this.state.name)
}
}
export default App
vim Demo.js
import React, { Component } from 'react'
import App from './App'
export default class Demo extends Component {
constructor(){
super();
this.state = {
msg: 'demo'
}
}
m(str){
this.setState({msg: str})
console.log('m...'+str)
}
render() {
return (
<div>
<h1>this demo's Component, msg={this.state.msg}</h1>
<App mesg={this.m.bind(this)}/>
</div>
)
}
}
结果:
默认情况下 msg=demo
单击 button 后,子组件 App 通过函数调用的方式将 msg 更改为 tom
3 注意事项
- 对于前端单击按钮操作,需要将其动作绑定到当前组件,否则会出现 App.js:25 Uncaught TypeError: Cannot read properties of undefined (reading ‘setState’) 的错误
- 对于数据状态更新的,需要通过 this.setState() 方法来更新。
- vscode 中安装 es7 组件后,可以通过 rcc rce rpc 等快捷方式生成基础代码
具体信息可以参考: ES7 React/Redux/GraphQL/React-Native snippets
4 说明
React基础入门+综合案例
react 官网
React基础入门教程
React组件的生命周期函数
React组件通信(父子组件传值)