<!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>
<style>
.title{
width: 200px;
background-color: coral;
}
</style>
</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 用于将ajax转为js -->
<script type="text/javascript" src="../js/babel.min.js"></script>
<!-- 引入prop-types 用于对组件标签属性进行限制 -->
<script type="text/javascript" src="../js/prop-types.js"></script>
<script type="text/babel">
class Demo extends React.Component{
constructor(props){
console.log('Demo--constructor');
super(props)
this.state = {
count:0
}
}
add=()=>{
const {count} = this.state
this.setState({count:count+1})
}
del = ()=>{
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
componentWillMount(){
console.log('Demo--componentDidMount');
}
componentDidMount(){
console.log('Demo--componentDidMount');
}
componentWillUnmount(){
console.log('Demo--componentWillUnmount');
}
shouldComponentUpdate(){
console.log('Demo--shouldComponentUpdate');
return true
}
componentWillUpdate(){
console.log('Demo--componentWillUpdate');
}
componentDidUpdate(preprops,prestate){
console.log('Demo--componentDidUpdate',preprops,prestate);
}
render() {
console.log('Demo--render');
return (
<div>
<h2>当前求和为:{this.state.count}</h2>
<button onClick={this.add} >点我+1</button>
<button onClick={this.del}>点我卸载当前组件</button>
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('test'))
</script>
</body>
</html>
新
尽量不用
getSnapshotBeforeUpdate()应用
实现滑动滑块后,页面更新时不改变当前显示内容 原理 在更新完毕之前也就是在didupdate之前 页面的高度是未更新的 所以 在更新时传入更新前的容器高度 当前容器的页面高度+= 用更新后的容器高度减去更新前的也就是getSnapshotBeforeUpdate()传入的高度 就实现当前效果了
<!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>
<style>
.list{
height: 100px;
overflow: auto;
width: 100px;
background-color: sandybrown;
}
.title{
width: 200px;
background-color: coral;
}
</style>
</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 用于将ajax转为js -->
<script type="text/javascript" src="../js/babel.min.js"></script>
<!-- 引入prop-types 用于对组件标签属性进行限制 -->
<script type="text/javascript" src="../js/prop-types.js"></script>
<script type="text/babel">
class Demo extends React.Component{
state={
newsArr:[]
}
componentDidMount(){
setInterval(()=>{
const {newsArr} = this.state
let 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((item,index)=>(
<div key={index}>{item}</div>
))}
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('test'))
</script>
</body>
</html>
|