constructor(props) {
super(props)
this.state = {
interval: null,
}
}
intervalToggle = () => {
this.getData()
this.state.interval && clearInterval(this.state.interval)
this.setState({
interval: setInterval(() => this.getData(), 1000 * 60 * 5),
})
}
componentDidMount() {
this.intervalToggle()
}
componentWillUnmount() {
clearInterval(this.state.Interval)
}
开发收获:
- 尽可能减少变量数-变量名规范命名
- 不传递不必要的参数
- 减少函数量和代码行数
- 提取公共组件-公共方法
const getSeeData = (isReset) => {
isReset && setEndCount(COUNT)
if (tabKey === '1') {
setSeeData(notifyData.slice(0, isReset ? COUNT : endCount))
} else {
setSeeData(infoData.slice(0, isReset ? COUNT : endCount))
}
}
useEffect(() => {
getSeeData(false)
}, [infoData, notifyData])
useEffect(() => {
getSeeData(true)
}, [tabKey])