简体   繁体   English

尝试刷新usestate时为什么程序会中断?

[英]Why does my program break when I try to refresh the usestate?

When I try to refresh the usestate my program just breaks. 当我尝试刷新usestate时,我的程序就中断了。 The problematic line is "setMesses(_messages);". 有问题的行是“ setMesses(_messages);”。 I tried to capitalized the usestate but nothing has changed. 我试图将使用状态大写,但没有任何变化。

import React, {useState} from 'react';
import Message from './Message';
import * as firebase from "firebase";

function MessContainer() {
    let counter = 0;
    let _messages = [];
    const [messes, setMesses] = useState([{this: null}]);

    firebase.database().ref().child('counter').on('value', function(snapshot){
        counter = snapshot.child("counter").val();
    });

    function load(_counter){
        firebase.database().ref().child('messages/' + _counter).on('value', function(snapshot){        
            let _chet = {}; 
            let _name = snapshot.child("name").val();

            _chet.mess =  _name + ": " + snapshot.child("message").val();

            if(_name === document.getElementById("name").value){
              _chet.status = "right";
            } else {
              _chet.status = "left";
            }
            _messages.push(_chet);
        });
    }


    function loadChet(){
        _messages = [];
        for(let i = 0; i < counter; i++){
            load(i);
        }
        console.log(_messages);
        setMesses(_messages);
        setTimeout(loadChet, 1000); 
    }

    loadChet();

    return (
        <div>{messes.map(_mess => (
            <Message mess={_mess.mess} status={_mess.status} />
        ))}
        </div>
    );
}

export default MessContainer;

The reason why this happens is because you call loadChet and this calls setMesses wich makes the component rerender and call loadChet again, causing a infinity loop. 发生这种情况的原因是因为您调用loadChet并调用setMesses wich使得组件重新渲染并再次调用loadChet ,从而导致无限循环。

You shouldn't call loadChet on the function, maybe use useEffect and call it only once will. 您不应该在函数上调用loadChet ,也许使用useEffect并只调用一次即可。 When do you need to call loadChet ? 您何时需要致电loadChet

Edit: 编辑:

Try this 尝试这个

function MessContainer() {
    let counter = 0;
    let _messages = [];
    const [messes, setMesses] = useState([{this: null}]);

    firebase.database().ref().child('counter').on('value', function(snapshot){
        counter = snapshot.child("counter").val();
    });

    function load(_counter){
        firebase.database().ref().child('messages/' + _counter).on('value', function(snapshot){        
            let _chet = {}; 
            let _name = snapshot.child("name").val();

            _chet.mess =  _name + ": " + snapshot.child("message").val();

            if(_name === document.getElementById("name").value){
              _chet.status = "right";
            } else {
              _chet.status = "left";
            }
            _messages.push(_chet);
        });
    }


    function loadChet(){
        _messages = [];
        for(let i = 0; i < counter; i++){
            load(i);
        }
        console.log(_messages);
        setMesses(_messages);
        setTimeout(loadChet, 1000); 
    }

    useEffect(() => {
        loadChet();
    }, [])

    return (
        <div>{messes.map(_mess => (
            <Message mess={_mess.mess} status={_mess.status} />
        ))}
        </div>
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将方法附加到类原型时,为什么代码会中断? - Why does my code break when I try to attach my methods to the class prototype? 当我尝试调试ASP.NET程序时,为什么Internet Explorer(或其他浏览器)使用旧的JavaScript文件? - Why does Internet Explorer (or other browsers) use old JavaScript files when I try to debug my ASP.NET program? 为什么在尝试清理事件处理程序时,该事件处理程序代码会中断? - Why does this event handling code for ender break when I try to clean it up some? 为什么我的 localStorage 变量会在页面刷新时中断? - Why does my localStorage variable break on a page refresh? 为什么我按下按钮时页面会刷新? - Why does my page refresh when I press buttons? "<i>Why does changing state (useState) break my p5.js code?<\/i>为什么更改状态 (useState) 会破坏我的 p5.js 代码?<\/b> <i>(React + p5.js application)<\/i> (反应 + p5.js 应用程序)<\/b>" - Why does changing state (useState) break my p5.js code? (React + p5.js application) 当我尝试刷新页面时出现此错误 - When i try to refresh my page i have this error 当我将 Tableau 示例中的 URL 更改为另一个 URL 时,为什么我的代码会中断? - Why does my code break when I change the URL in the Tableau sample to another URL? 当我将脚本类型设置为模块时,为什么我的 D3 代码会中断? - Why does my D3 code break when I set the script type to module? 为什么当我第二次将“q”传递给我的输入时,它不会中断循环? - Why when I pass "q" the second time into my input it does not break the loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM