一、引出生命周期
1.1、案例目标效果
需求:定义组件实现以下功能:
- 让指定的文本做显示 / 隐藏的渐变动画(挂载组件)
- 从完全可见,到彻底消失,耗时2S
- 点击“不活了”按钮从界面中卸载组件
1.2、卸载组件
ReactDOM.unmountComponentAtNode(document.getElementById("test"))
1.3、挂载组件
componentDidMount() {
setInterval(() => {
let { opacity } = this.state
opacity -= 0.1
if (opacity <= 0) {
opacity = 1
}
this.setState({ opacity: opacity })
}, 200)
}
点击卸载组件 报错 原因是组建已经卸载了,定时器里面还在更新状态,所以要在卸载组件之前要清空定时器 需要拿到定时器的id才能清除定时器,把定时器挂在组建实例对象上 this.timerId= setInterval(() => {},这样清空定时器可以通过实例取到timerId—clearInterval(this.timerId)
return (
<div>
<h2 style={{ opacity: this.state.opacity }}>落霞与孤鹜齐飞,秋水共长天一色</h2>
<button onClick={this.death}>卸载组件</button>
</div>
)
state = { opacity: 1 }
death = () => {
clearInterval(this.timerId)
ReactDOM.unmountComponentAtNode(document.getElementById("test"))
}
componentDidMount() {
this.timerId= setInterval(() => {
let { opacity } = this.state
opacity -= 0.1
if (opacity <= 0) {
opacity = 1
}
this.setState({ opacity: opacity })
}, 200)
}
1.4、组件将要被卸载
组件将要被卸载的时候执行
componentWillUnmount() {
clearInterval(this.timerId)
}
这段方法一样可以在组件卸载之前清空定时器
1.5、完整代码
<!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>hello_react</title>
</head>
<body>
<!-- 准备好一个容器 -->
<div id='test'></div>
<!-- 引入react核心库 -->
<script type="text/javascript" src="../js/react.development.js"></script>
<!-- 引入react-dmo 用于react操作DOM -->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<!-- 引入babel 用于将jsx转为js -->
<script type="text/javascript" src="../js/babel.min.js"></script>
<!-- 此处一定要写babel -->
<script type="text/babel">
class Life extends React.Component {
state = { opacity: 1 }
death = () => {
ReactDOM.unmountComponentAtNode(document.getElementById("test"))
}
componentDidMount() {
this.timerId = setInterval(() => {
let { opacity } = this.state
opacity -= 0.1
if (opacity <= 0) {
opacity = 1
}
this.setState({ opacity: opacity })
}, 200)
}
componentWillUnmount() {
clearInterval(this.timerId)
}
render() {
return (
<div>
<h2 style={{ opacity: this.state.opacity }}>落霞与孤鹜齐飞,秋水共长天一色</h2>
<button onClick={this.death}>卸载组件</button>
</div>
)
}
}
ReactDOM.render(<Life />, document.getElementById('test'))
</script>
</body>
</html>
1.6、引出生命周期的目的
为了让我们知道代码执行过程中会在某个特定的时间点 执行特定的事
二、生命周期(旧)
2.1、💋setState 流程💋
setState
add = () => {
const { count } = this.state
this.setState({ count: count + 1 })
console.log('Count-----setState');
}
shouldComponentUpdate
shouldComponentUpdate(){
console.log('Count-----shouldComponentUpdate')
return true
}
componentWillUpdate
componentWillUpdate(){
console.log('Count-----componentWillUpdate')
}
componentDidUpdate
componentDidUpdate(){
console.log('Count-----componentDidUpdate')
}
render
render() {
console.log('Count-----render');
}
componentWillUnmount
componentWillUnmount() {
console.log('Count-----componentWillUnmount')
}
完整代码
<!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>hello_react</title>
</head>
<body>
<!-- 准备好一个容器 -->
<div id='test'></div>
<!-- 引入react核心库 -->
<script type="text/javascript" src="../js/react.development.js"></script>
<!-- 引入react-dmo 用于react操作DOM -->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<!-- 引入babel 用于将jsx转为js -->
<script type="text/javascript" src="../js/babel.min.js"></script>
<!-- 此处一定要写babel -->
<script type="text/babel">
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 })
console.log('Count-----setState');
}
componentWillMount(){
console.log('Count-----componentWillMount');
}
componentDidMount(){
console.log('Count-----componentDidMount');
}
death = () => {
ReactDOM.unmountComponentAtNode(document.getElementById("test"))
}
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>
<h1>当前求和为:{count}</h1>
<button onClick={this.add}>点击我1+</button>
<button onClick={this.death}>卸载组件</button>
</div>
)
}
}
ReactDOM.render(<Count />, document.getElementById('test'))
</script>
</body>
</html>
2.2、💋forceUpdate 流程💋
forceUpdate
force=()=>{
this.forceUpdate()
console.log('Count-----forceUpdate');
}
componentWillUpdate
componentWillUpdate(){
console.log('Count-----componentWillUpdate')
}
render
render() {
console.log('Count-----render');
}
componentDidUpdate
componentDidUpdate(){
console.log('Count-----componentDidUpdate')
}
componentWillUnmount
componentWillUnmount() {
console.log('Count-----componentWillUnmount')
}
<!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>hello_react</title>
</head>
<body>
<!-- 准备好一个容器 -->
<div id='test'></div>
<!-- 引入react核心库 -->
<script type="text/javascript" src="../js/react.development.js"></script>
<!-- 引入react-dmo 用于react操作DOM -->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<!-- 引入babel 用于将jsx转为js -->
<script type="text/javascript" src="../js/babel.min.js"></script>
<!-- 此处一定要写babel -->
<script type="text/babel">
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 })
console.log('Count-----setState');
}
componentWillMount(){
console.log('Count-----componentWillMount');
}
componentDidMount(){
console.log('Count-----componentDidMount');
}
death = () => {
ReactDOM.unmountComponentAtNode(document.getElementById("test"))
}
force=()=>{
this.forceUpdate()
console.log('Count-----forceUpdate');
}
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>
<h1>当前求和为:{count}</h1>
<button onClick={this.add}>点击我1+</button>
<button onClick={this.death}>卸载组件</button>
<button onClick={this.force}>不更改任何状态中的数据,强制更新</button>
</div>
)
}
}
ReactDOM.render(<Count />, document.getElementById('test'))
</script>
</body>
</html>
2.3、💋父组件render 流程💋
案例效果(B组件继承A组件)
render
render(){
console.log('B---render');
return(
<div>我是B组件,接收到的车是:{this.props.carName}</div>
)
}
componentWillReceiveProps
componentWillReceiveProps(props){
console.log('B---componentWillReceiveProps',props);
}
shouldComponentUpdate
shouldComponentUpdate(){
console.log('B---shouldComponentUpdate');
return true
}
componentWillUpdate
componentWillUpdate(){
console.log('B---componentWillUpdate');
}
componentDidUpdate
componentDidUpdate(){
console.log('B---componentDidUpdate');
}
componentWillUnmount
componentWillUnmount(){
console.log('B---componentWillUnmount');
}
完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>react生命周期(旧)render父组件</title>
</head>
<body>
<!-- 准备好一个“容器” -->
<div id="test"></div>
<!-- 引入react核心库 -->
<script type="text/javascript" src="../js/react.development.js"></script>
<!-- 引入react-dom,用于支持react操作DOM -->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<!-- 引入babel,用于将jsx转为js -->
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">
class A extends React.Component{
state = {carName:'奔驰'}
changeCar = ()=>{
this.setState({carName:'宝马'})
}
death1 = ()=>{
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
render(){
console.log('A----render');
return(
<div>
<div>我是A组件</div>
<button onClick={this.changeCar}>换车</button>
<button onClick={this.death1}>卸载组件</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');
}
componentWillUnmount(){
console.log('B---componentWillUnmount');
}
render(){
console.log('B---render');
return(
<div>我是B组件,接收到的车是:{this.props.carName}</div>
)
}
}
ReactDOM.render(<A/>,document.getElementById('test'))
</script>
</body>
</html>
2.4、总结
a、 初始化阶段 : 由ReactDOM.render()触发—初次渲染 1. constructor() 2. componentWillMount() 3. render() 4. componentDidMount() =====> 常用 一般在这个钩子中做一些初始化 的事,例如:开启定时器 、发送网络请求 、订阅消息 等等
b、 更新阶段 : 由组件内部this.setSate() 或父组件render 触发 1. shouldComponentUpdate() 2. componentWillUpdate() 3. render() =====> 必须使用的一个 4. componentDidUpdate()
c、 卸载组件 : 由ReactDOM.unmountComponentAtNode() 触发 1. componentWillUnmount() =====> 常用 一般在这个钩子中做一些收尾 的事,例如:关闭定时器 、取消订阅消息 等等
三、生命周期(新)
3.1、生命周期更新(UNSAFE_ )
由于React官方更新了,所以生命周期也更新了,相应的包也更新了 Count类 所以新的生命周期执 行代码会爆黄 ,需要在它提示的对应组件加上UNSAFE_
UNSAFE_componentWillMount(){
console.log('Count---componentWillMount');
}
UNSAFE_componentWillUpdate(){
console.log('Count---componentWillUpdate');
}
A类、B类
UNSAFE_componentWillReceiveProps(props){
console.log('B---componentWillReceiveProps',props);
}
UNSAFE_componentWillUpdate(){
console.log('B---componentWillUpdate');
}
完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>react生命周期(新)</title>
</head>
<body>
<!-- 准备好一个“容器” -->
<div id="test"></div>
<!-- 引入react核心库 -->
<script type="text/javascript" src="../js1/react.development.js"></script>
<!-- 引入react-dom,用于支持react操作DOM -->
<script type="text/javascript" src="../js1/react-dom.development.js"></script>
<!-- 引入babel,用于将jsx转为js -->
<script type="text/javascript" src="../js1/babel.min.js"></script>
<script type="text/babel">
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()
}
UNSAFE_componentWillMount(){
console.log('Count---componentWillMount');
}
componentDidMount(){
console.log('Count---componentDidMount');
}
componentWillUnmount(){
console.log('Count---componentWillUnmount');
}
shouldComponentUpdate(){
console.log('Count---shouldComponentUpdate');
return true
}
UNSAFE_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:'宝马'})
}
death1 = ()=>{
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
render(){
console.log('A----render');
return(
<div>
<div>我是A组件</div>
<button onClick={this.changeCar}>换车</button>
<button onClick={this.death1}>卸载组件</button>
<B carName={this.state.carName}/>
</div>
)
}
}
class B extends React.Component{
UNSAFE_componentWillReceiveProps(props){
console.log('B---componentWillReceiveProps',props);
}
shouldComponentUpdate(){
console.log('B---shouldComponentUpdate');
return true
}
UNSAFE_componentWillUpdate(){
console.log('B---componentWillUpdate');
}
componentDidUpdate(){
console.log('B---componentDidUpdate');
}
componentWillUnmount(){
console.log('B---componentWillUnmount');
}
render(){
console.log('B---render');
return(
<div>我是B组件,接收到的车是:{this.props.carName}</div>
)
}
}
ReactDOM.render(<A/>,document.getElementById('test'))
</script>
</body>
</html>
|