react组件中保存了localStorage的pagecount1的值点击后会改变是正常的吗?请大神分析一下。
源码如下:
import React, { Component } from 'react'
import ReactDom from 'react-dom'
import Style from './main.css'
export default class LikeButton extends Component {
constructor() {
super();
this.state = {
liked: false
};
}
handleClick() {
this.setState({liked: !this.state.liked});
}
render() {
const text = this.state.liked ? 'liked' : 'haven\'t liked';
const style = this.state.liked ? { background: '#8aa'} : {};
localStorage.pagecount1 = localStorage.pagecount1 ? Number(localStorage.pagecount1) + 1 : 1;
return (
<div className={Style.box}>
<div style={style} className={Style.btn} onClick={this.handleClick.bind(this)} title={'Click to toggle'}>
You {text} button.
</div>
<div> "访问页面次数:{localStorage.pagecount1}次"</div>
</div>
);
}
}
小唯快跑啊
相关分类