Files
react/basic/6/P07.html
T
2022-05-01 19:27:52 +08:00

74 lines
2.2 KiB
HTML

<!--
* @Author: chenxuan
* @Date: 2022-01-13 23:49:27
* @LastEditTime: 2022-01-14 00:40:51
* @LastEditors: chenxuan
-->
<!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>Document</title>
<title>getSnapshotBeforeUpdate</title>
<script src="../package/new/react.development.js"></script>
<script src="../package/new/react-dom.development.js"></script>
<script src="../package/new/babel.min.js"></script>
<style>
.list{
width: 150px;
height: 150px;
border: 1px solid red;
overflow-y: auto;
}
.list li{
width: 100%;
list-style: none;
height: 30px;
line-height: 30px;
}
</style>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
class Count extends React.Component{
state={news:["新闻"]}
componentDidMount(){
setInterval(()=>{
const {news}=this.state;
const news_="新闻"+(news.length+1);
news.unshift(news_);
this.setState({news:news})
},1000)
}
getSnapshotBeforeUpdate(){
return this.refs.list.scrollHeight;
}
componentDidUpdate(preProps,preState,height){
this.refs.list.scrollTop+=this.refs.list.scrollHeight-height
}
render(){
// console.log(this.state.news)
return (
<div className="list" ref="list">
{
this.state.news.map((item,index)=>{
return (
<li key={index}>{item}</li>
)
})
}
</div>
)
}
}
ReactDOM.render(
<Count count={1999} />,document.getElementById('app')
)
</script>
</body>
</html>