账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react里这种报错该怎么解决
    28
    0

    报错提示如下:

    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. Please check the code for the Timer component.

    这里是我的代码(这里的props是接受另外一个组建传过来的数据):

    class Timer extends PureComponent {
    
        static propTypes = {
            aim: PropTypes.number
    
        };
        constructor(props) {
            super(props);
            this.state = {
                num: 0,
                days: 0
            }
        }
        tick() {
            if (this.props.aim) {
                const aimTime = new Date(this.props.aim);
                const currentTime = Date.parse(new Date());
                const leftTime = aimTime - currentTime;
                const day = aimTime.getDate();
                const hours = aimTime.getHours();
                this.setState({
                    num: hours,
                    days: day
                }, () => {
                    if (currentTime === this.props.aim) {
                        clearInterval(this.interval);
                    }
                });
            }else{
                this.setState({
                    num: 0,
                    days: 0
                })
            }
        }
        componentDidMount() {
            this.interval = setInterval(() => this.tick(), 1000);
        }
        componentWillMount(){
           clearInterval(this.interval);
            
        }
        render() {
    
            return (
                <span>还剩 {this.state.days} 天 {this.state.num} 小时</span>
            );
        }
    }

    这里附上stackflow里的答案,但是看了下不是很清楚。点击链接

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 清风不解语 普通会员 1楼

      在React中,报错的解决方式有很多种,以下是一些常见的解决方式:

      1. 检查错误信息:在你的代码中,通常会有一段错误信息,比如 "TypeError: Cannot read property 'key' of undefined"。你需要检查这个错误信息,看看错误的具体原因是什么。

      2. 检查DOM:如果你的组件在使用DOM时出了问题,那么你需要检查这个DOM元素是否在正确的位置,是否有空的属性,或者是否有其他错误。

      3. 使用console.log:你可以使用console.log()函数来打印出错误的堆栈信息,这样可以帮助你找出错误的具体位置。

      4. 使用React的错误处理:React提供了一套完整的错误处理机制,比如使用try/catch语句来捕获错误,然后使用React的catch()函数来处理这些错误。

      5. 使用第三方库:如果你的代码中使用了第三方库,那么你需要检查这个库是否正常工作,是否有错误。

      6. 代码审查:如果你的代码是开源的,那么你可能需要进行代码审查,让其他人检查你的代码,看看是否有错误。

      7. 重新学习和理解React:如果你的错误原因复杂,那么可能需要重新学习和理解React,看看是否可以通过更深入的理解来解决这个问题。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部