需求:定义组件实现以下功能:
//1、创建组件
class Demo extends React.Component {
state = {opacity: 1}
death = () => {
//卸载组件
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
//组件完成挂载后执行
componentDidMount() {
this.timer=setInterval(() => {
// 获取原状态
let {opacity} = this.state;
// 减小0.1
opacity -= 0.1
if (opacity <= 0) opacity = 1
this.setState({opacity})
}, 200)
}
//组件将要卸载时调用
componentWillUnmount(){
//清除定时器
clearInterval(this.timer)
}
//render调用时机:初始化渲染、状态更新之后
render() {
return (
{opacity: this.state.opacity}}>react感觉还是有点难滴!!!怎么办?
)
}
}
//2、渲染虚拟DOM
ReactDOM.render( , document.getElementById('test'))

初始化阶段: 由ReactDOM.render()触发—初次渲染
更新阶段: 由组件内部this.setSate()或父组件重新render触发
卸载组件: 由ReactDOM.unmountComponentAtNode()触发
//1、创建组件
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');
}
//将要卸载
componentWillUnmount() {
//清除定时器
console.log('count-componentWillUnmount');
}
//控制组件是否更新的”阀门“
shouldComponentUpdate() {
console.log('count-shouldComponentUpdate');
return true;//返回false状态无法更新,返回值为true,则更新
}
//组件将要更新
componentWillUpdate() {
console.log('count-componentWillUpdate');
}
//组件更新完成
componentDidUpdate() {
console.log('count-componentDidUpdate');
}
//count+1
add = () => {
// 获取原状态
let {count} = this.state;
this.setState({
count: count + 1
})
}
//卸载组件
unmount = () => {
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
//强制更新
force = () => {
//强制更新(无 控制组件是否更新的‘阀门’的步骤)
this.forceUpdate();
}
render() {
console.log('count-render');
let {count} = this.state;
return (
当前求和为:{count}
)
}
}
//父组件
class Father extends React.Component {
state = {carName: '宝马'}
changeCar = () => {
this.setState({
carName: '马自达'
})
}
render() {
return (
Father
)
}
}
//子组件
class Child extends React.Component {
//组件将要接收父组件传过来的新的属性值(第一次不会执行)
componentWillReceiveProps(props) {
console.log('Child-componentWillReceiveProps',props);
}
render() {
return (我是Child组件,我从Father组件中接收到的车是:{this.props.carName})
}
}
//2、渲染虚拟DOM
// ReactDOM.render( , document.getElementById('test'))
ReactDOM.render( , document.getElementById('test'))

现在使用会出现警告,需要加上UNSAFE_前缀才能使用,以后可能会被彻底废弃,不建议使用。
初始化阶段: 由ReactDOM.render()触发—初次渲染
更新阶段: 由组件内部this.setSate()或父组件重新render触发
卸载组件: 由ReactDOM.unmountComponentAtNode()触发
//1、创建组件
class Count extends React.Component {
//构造器
constructor(props) {
console.log('count-constructor');
super(props);
this.state = {count: 0}
}
// 若state的值在任何时候都取决于props,可以使用
//派生状态(state的值在任何时候都取决于props.派生状态会导致代码冗余,并使组件难以维护)
static getDerivedStateFromProps(props, state) {
//必须有返回值,null或state obj状态对象
console.log('count-getDerivedStateFromProps', props, state);
return null;
// return props;//返回状态对象会影响状态的更新
}
getSnapshotBeforeUpdate(prevProps, prevState) {
console.log('count-getSnapshotBeforeUpdate', prevProps, prevState);
return 'xiao';
}
//挂载后
componentDidMount() {
console.log('count-componentDidMount');
}
//将要卸载
componentWillUnmount() {
//清除定时器
console.log('count-componentWillUnmount');
}
//控制组件是否更新的”阀门“
shouldComponentUpdate() {
console.log('count-shouldComponentUpdate');
return true;//返回false状态无法更新,返回值为true,则更新
}
//组件更新完成(第三个参数为getSnapshotBeforeUpdate钩子函数的返回值)
componentDidUpdate(prevProps, prevState,snapshotValue) {
console.log('count-componentDidUpdate',prevProps, prevState,snapshotValue);
}
//count+1
add = () => {
// 获取原状态
let {count} = this.state;
this.setState({
count: count + 1
})
}
//卸载组件
unmount = () => {
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
render() {
console.log('count-render');
let {count} = this.state;
return (
当前求和为:{count}
)
}
}
//2、渲染虚拟DOM
ReactDOM.render( , document.getElementById('test'))
getSnapshotBeforeUpdate的应用场景:
以特殊方式处理滚动位置
//1、创建组件
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(prevProps, prevState, snapshotValue) {
this.refs.list.scrollTop += this.refs.list.scrollHeight - snapshotValue;
}
render() {
return (
{this.state.newsArr.map((item, index) => {
return {item}
})}
)
}
}
ReactDOM.render( , document.getElementById('test'))

不断产生新的数据,滚动条的高度不断变小,但是显示的内容不会随新产生的数据而滚动