react踩到的坑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26983555/article/details/79753854

Warning: setState(…): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.

一开始一直以为是在componentDidMount组件里面写到了this.setTate(),导致出现这个问题的缘故,后来才知道是组件被卸载定时器还在运行的原因。好了不说,直接上代码。

componentDidMount() {
 setInterval(() => {
      this.setState({
        currentDate: moment().format('YYYY-MM-DD HH:mm:ss'),
      });
    }, 1000);
 }

其实这段代码,看起来是没有问题的,问题就出在生命周期里面。componentDidMount和componentWillUnmount。在组件被卸载的时候,还在执行定时器。所以需要修改成这样:

this.setTime = setInterval(() => {
this.setState({
currentDate: moment().format(‘YYYY-MM-DD HH:mm:ss’),
});
}, 1000);
componentWillUnmount() {
clearInterval(this.setTime);
}
“`

上传多张图片

antd框架 handleChange = ({ fileList }) => this.setState({ fileList })而实际要让图片显示出来,还需要设置this.setState({
goodsImg: info.file.response.linkurl,
});

猜你喜欢

转载自blog.csdn.net/qq_26983555/article/details/79753854