一、介绍生命周期
需求: 1、文字从透明度为1变为0,反复执行这个动画 2、点击按钮将页面清空(卸载组件)
<!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>生命周期</title>
</head>
<body>
<div id="test"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class Demo extends React.Component {
state = { opacity: 1 }
death = () => {
ReactDOM.unmountComponentAtNode(document.querySelector('#test'))
}
componentWillMount() {
console.log(1)
clearInterval(this.timer)
}
componentDidMount() {
this.timer = setInterval(() => {
let { opacity } = this.state;
opacity -= 0.1;
if (opacity <= 0) opacity = 1;
this.setState({ opacity });
}, 200);
}
render() {
console.log('render')
return (
<div>
<h2 style={{ opacity: this.state.opacity }}>React?</h2>
<button onClick={this.death}>卸载</button>
</div>
)
}
}
ReactDOM.render(<Demo />, document.querySelector('#test'))
</script>
</body>
</html>
二、生命周期(旧)
16x版本的生命周期顺序:
<!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>生命周期</title>
</head>
<body>
<div id="test"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class Demo extends React.Component {
constructor(props) {
console.log('最开始:constructor')
super(props)
this.state = { Count: 0 }
}
componentWillMount() {
console.log('组件将要挂载,componentWillMount')
}
render() {
console.log('组件渲染的时候,状态更新之后:render')
const { Count } = this.state;
return (
<div>
<h2>求和为{Count}</h2>
<button onClick={this.add}>加一</button>
<button onClick={this.death}>卸载组件</button>
<button onClick={this.force}>强制更新</button>
</div>
)
}
componentDidMount() {
console.log('组件挂载完成:componentDidMount')
}
shouldComponentUpdate() {
console.log('控制组件更新的“阀门”:shouldComponentUpdate')
return true
}
componentWillUpdate() {
console.log('组件将要更新:componentWillUpdate')
}
componentDidUpdate() {
console.log('组件更新完成:componentDidUpdate')
}
componentWillUnmount() {
console.log('组件将要卸载:componentWillUnmount')
}
death = () => {
ReactDOM.unmountComponentAtNode(document.querySelector('#test'))
}
force = () => {
this.forceUpdate()
}
add = () => {
let { Count } = this.state;
Count++;
this.setState({ Count })
}
}
ReactDOM.render(<Demo />, document.querySelector('#test'))
</script>
</body>
</html>
子父组件的生命周期(componentWillReceiveProps)
<!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>生命周期(子父组件)</title>
</head>
<body>
<div id="test"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class Parent extends React.Component {
state = { name: '奔驰' }
change = () => {
this.setState({ name: '宝马' })
}
render() {
console.log('父组件渲染的时候,状态更新之后:render')
return (
<div>
<div>parent</div>
<button onClick={this.change}>切换</button>
<Child name={this.state.name} />
</div>
)
}
}
class Child extends React.Component {
componentWillReceiveProps() {
console.log('子组件将要接收父组件的Props:componentWillReceiveProps')
}
componentDidMount() {
console.log('子组件挂载完成:componentDidMount')
}
render() {
console.log('子组件渲染的时候,状态更新之后:render')
return (
<div>
<div>child:{this.props.name}</div>
</div>
)
}
shouldComponentUpdate() {
console.log('控制子组件更新的“阀门”:shouldComponentUpdate')
return true
}
componentWillUpdate() {
console.log('子组件将要更新:componentWillUpdate')
}
componentDidUpdate() {
console.log('子组件更新完成:componentDidUpdate')
}
componentWillUnmount() {
console.log('子组件将要卸载:componentWillUnmount')
}
}
ReactDOM.render(<Parent />, document.querySelector('#test'))
</script>
</body>
</html>
三、生命周期(新)
17x版本的生命周期顺序:
<!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>17版本生命周期</title>
</head>
<body>
<div id="test"></div>
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class Demo extends React.Component {
constructor(props) {
console.log('最开始:constructor')
super(props)
this.state = { count: 0 }
}
static getDerivedStateFromProps(props, state) {
console.log('getDerivedStateFromProps', props)
return null
}
getSnapshotBeforeUpdate() {
console.log('getSnapshotBeforeUpdate')
return '快照值'
}
render() {
console.log('组件渲染的时候,状态更新之后:render')
const { count } = this.state;
return (
<div>
<h2>求和为{count}</h2>
<button onClick={this.add}>加一</button>
<button onClick={this.death}>卸载组件</button>
<button onClick={this.force}>强制更新</button>
</div>
)
}
componentDidMount() {
console.log('组件挂载完成:componentDidMount')
}
shouldComponentUpdate() {
console.log('控制组件更新的“阀门”:shouldComponentUpdate')
return true
}
componentDidUpdate(perProps,preState,snapValue) {
console.log('组件更新完成:componentDidUpdate',perProps,preState,snapValue)
}
componentWillUnmount() {
console.log('组件将要卸载:componentWillUnmount')
}
death = () => {
ReactDOM.unmountComponentAtNode(document.querySelector('#test'))
}
force = () => {
this.forceUpdate()
}
add = () => {
let { count } = this.state;
count++;
this.setState({ count })
}
}
ReactDOM.render(<Demo count={199} />, document.querySelector('#test'))
</script>
</body>
</html>
1.初始化阶段:由ReactDOM。render()触发—初次渲染 ????1.consructor() ????2.getDerivedStateFormProps ????3.render() ????4.componentDidMount() =>常用 ????一般在这个钩子中做一些初始化的事情,比如;开启定时器、发送网络请求、订阅消息 2.更新阶段:由组件内部this.setState()或父组件重新render触发 ????1.getDerivedStateFromProps ????2.shouldComponentUpdate() ????3.render() ????4.getSnapshotBeforeUpdate ????5.componentDidUpdate() 3.卸载组件:由ReactDOM.unmountComponentAtNode()触发 ????1.componentWillUnmount() =>常用 ????一般在这个钩子中做一些收尾的事情,比如关闭定时器、取消订阅消息
新增的两个钩子:
- getDerivedStateFromProps
static getDerivedStateFromProps(props, state) {
console.log('getDerivedStateFromProps', props)
return null
}
- getSnapshotBeforeUpdate
getSnapshotBeforeUpdate() {
console.log('getSnapshotBeforeUpdate')
return '快照值'
}
四、新旧的区别
回答方式:新的生命周期和旧的生命周期相比,它废弃了3个钩子:componentWillMount、componentWillReceiveProps、componentWillUpdate,同时又提出两个新的钩子:getDerivedStateFromProps、getSnapshotBeforeUpdate。
五、重要的钩子(常用)
1.render:初始化渲染或更新渲染调用 2.componentDidMount:开启监听,发送ajax请求 3.componentWillUnmount:做一些收尾的工作,如:清除定时器
六、即将废弃的钩子
1.componentWillMount 2.componentWillReceiveProps 3.componentWillUpdate
现在使用会出现警告,需要加上UNSAFE_前缀才能使用,以后可能会直接废弃。
以上就是React 中生命周期的内容,请大家关注《React 全家桶》专栏。 我会将自己平时项目中常见的问题以及笔试面试的知识在CSDN与大家分享,一起进步,加油。
|