总结一下最近用react写项目时,遇到的一些坑,恩,真的还蛮坑的,主要是设置状态的时候特别不好控制,下面我们一起来看下,这里自己做了几个demo,分别看下,
主页面代码如下:
class Head extends React.Component {
constructor(props) {
super(props);
this.state = {
contentClass:"conditionArea"
};
this.windowOnScroll();
let isScrollTop = true;
};
windowOnScroll(){
let _this = this;
window.onscroll = function(){
//获取滚动条滚动的距离
let h = document.body.scrollTop;
console.log(h);
if(h > 74){
console.log('111');
_this.setState({
contentClass:"conditionArea conditionArea_fixed"
});
}else{
_this.setState({
contentClass:"conditionArea"
});
}
}
};
render() {
return (
我是头部
置顶块
内容区域
);
}
};
function APP (){
return (