访问官网 ReactDOM 了解更多DOM 的特定方法

当前版本虽然依然全部可用,但是自16.x版本就开始说要删除其中的
componentWillMount、componentWillReceiveProps、componentWillUpdate;而且这三个钩子也基本万年不用一次,建议不要使用
constructor()
componentWillMount():组件挂载前置;组件将被挂载
render() 必用
componentDidMount():组件挂载完毕 常用
(prevProps, prevState),既挂载前收到的props和初始化时创建的stateshouldComponentUpdate():组件更新阀门,判断是否进行更新
(nextProps, nextState)forceUpdate()绕过阀门进行强制更新componentWillUpdate():组件更新前置;组件将要更新
(prevProps, prevState, snapshot)render() 必用
componentDidUpdate():组件更新完毕
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2_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">
//创建组件
class Count extends React.Component {
//构造器
constructor(props) {
console.log('Count---constructor');
super(props)
//初始化状态
this.state = { count: 0 }
}
//组件将要挂载的钩子
componentWillMount() {
console.log('Count--组件挂载前置--componentWillMount');
}
//组件挂载完毕的钩子
componentDidMount() {
console.log('Count--组件挂载完毕--componentDidMount');
}
//控制组件更新的“阀门”
shouldComponentUpdate() {
console.log('Count--组件更新阀门--shouldComponentUpdate');
return true // 必须返回boolean且只有返回true才会继续执行更新流程
}
//组件将要更新的钩子
componentWillUpdate() {
console.log('Count--组件更新前置--componentWillUpdate');
}
//组件更新完毕的钩子
componentDidUpdate() {
console.log('Count--组件更新完毕--componentDidUpdate');
}
//组件将要卸载的钩子
componentWillUnmount() {
console.log('Count--组件卸载前置--componentWillUnmount');
}
//加1按钮的回调
add = () => {
//获取原状态
const { count } = this.state
//更新状态
this.setState({ count: count + 1 })
}
//卸载组件按钮的回调
death = () => {
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
//强制更新按钮的回调
force = () => {
this.forceUpdate() // 绕过更新阀门直接强制更新
}
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 />, document.getElementById('test'))
script>
body>
html>

//父组件A
class A extends React.Component {
//初始化状态
state = { carName: '奔驰' }
changeCar = () => {
this.setState({ carName: '奥拓' })
}
render() {
return (
<div>
<div>我是A组件</div>
<button onClick={this.changeCar}>换车</button>
<hr />
<B carName={this.state.carName} />
</div>
)
}
}
//子组件B
class B extends React.Component {
//组件将要接收新的props的钩子
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(<A/>, document.getElementById('test'))


如果在新版本想继续使用这三个废弃的钩子,则必须在前面加上 “UNSAFE_” 前缀,这里的 “unsafe” 不是指安全性,而是表示使用这些生命周期的代码在 React 的未来版本中更有可能出现 bug,尤其是在启用异步渲染之后。具体可参考官博 异步渲染之更新(为未来规划计)。
(props, state)static 静态方法state 对象或者 nullcomponentDidUpdate()。此用法并不常见,但它可能出现在 UI 处理中,如需要以特殊方式处理滚动位置的聊天线程等。
(prevProps, prevState)null新钩子已经红色标出,由于根本不用,所以未加粗突出,甚至完全可以忽略这俩小透明
constructor()
static getDerivedStateFromProps()
render() 必用
componentDidMount() 常用
static getDerivedStateFromProps()
shouldComponentUpdate()
render() 必用
getSnapshotBeforeUpdate
componentDidUpdate()
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>4_getSnapShotBeforeUpdate的使用场景title>
<style>
.list{
width: 200px;
height: 150px;
background-color: skyblue;
overflow: auto;
}
.news{
height: 30px;
}
style>
head>
<body>
<div id="test">div>
<script type="text/javascript" src="../js/17.0.1/react.development.js">script>
<script type="text/javascript" src="../js/17.0.1/react-dom.development.js">script>
<script type="text/javascript" src="../js/17.0.1/babel.min.js">script>
<script type="text/babel">
class NewsList extends React.Component{
state = {newsArr:[]}
componentDidMount(){
setInterval(() => {
//获取原状态
const {newsArr} = this.state
//模拟一条新闻
const news = '新闻'+ (newsArr.length+1)
//更新状态
this.setState({newsArr:[news,...newsArr]})
}, 1000);
}
getSnapshotBeforeUpdate(){
return this.refs.list.scrollHeight
}
componentDidUpdate(preProps,preState,height){
this.refs.list.scrollTop += this.refs.list.scrollHeight - height
}
render(){
return(
<div className="list" ref="list">
{
this.state.newsArr.map((n,index)=>{
return <div key={index} className="news">{n}</div>
})
}
</div>
)
}
}
ReactDOM.render(<NewsList/>,document.getElementById('test'))
script>
body>
html>
滑动到哪就可以让内容停在哪,不会随着数据增加而被压到底下

小白学习参考视频:尚硅谷React教程
中文官网:State & 生命周期