简体   繁体   English

React.js + Socket.io 更新 state 更改列表项的 position

[英]React.js + Socket.io updating state changes position of list items

export default class extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            status: [],
            services: []
        }

        getAppData((err,opt, data) => {
            function Exists(list, id) {
                return list.some(function(el) {
                  return el.data.id == id;
                }); 
            }
            if (opt == "sysinfo"){
                var filtered = this.state.status;

                if (Exists(filtered, data.id)){
                    filtered = this.state.status.filter(function(el) { return el.data.id != data.id; }); 
                }
                
                filtered.push({ data })
                this.setState({status: filtered})
            } else if (opt == "init_services"){
                this.setState({services: data})
            }
            
        });
    }
    
    render() {
        

        const timestampforuse = this.state.status
        const totalList = this.state.services
        console.log(totalList)
        const mainList = totalList.map((link) =>
            <ListGroup.Item  key={link.id} keyProp={link.id}>Name: {link.name} Node: {link.node}</ListGroup.Item> 
        );
        console.log(totalList)
        const listItems = timestampforuse.map((link) =>
            <ListGroup.Item ><p key={link.data.id}>ID: {link.data.pid} Node: {link.data.node} <br/>Ram usage: {link.data.p_ram.toFixed(2)} / 100% Cpu usage: {link.data.p_cpu.toFixed(2)} / 100%</p></ListGroup.Item> 
        );

        return (
                <div>
                    <ListGroup>
                        {mainList}
                    </ListGroup>
                </div>
        );
    }
}

Data from sysinfo:来自 sysinfo 的数据:

{
            cores: 16,
            cpu: 0,
            id: "00ffab6ca93243f08eb10670d9c491d54cf674173d13c24a0a663ebb3f5e54d042ae",
            node: "1",
            p_cpu: 0,
            p_ram: 0.18230482881430612,
            pid: 29216,
            ram: 28.78515625,
            threads: 5,
            time: 1609179904302,
            time_from_startup: 1609179876.271594,
            time_since_boot: 1608562209.0201786
        }

Data for init:初始化数据:

add_game: true
description: "a test script"
id: "00ffab6ca93243f08eb10670d9c491d54a0a663ebb3f5e54d042ae"
name: "test331112321"
node: "1"

Socket script:套接字脚本:

import openSocket from 'socket.io-client';
const  socket = openSocket('http://localhost:3000');

function getAppData(cb) {
    socket.on('update_system', data => cb(null,"sysinfo", data));
    socket.on('init_services', data => cb(null,"init_services", data));
    socket.emit('updated', 1000);
}

export { getAppData };

I have tried using a map and using it as a list but when it updates every second it updates too fast to even read.我曾尝试使用 map 并将其用作列表,但是当它每秒更新一次时,它更新得太快,甚至无法阅读。 How would I make the name appear, then once data gets sent have that update the list but not update the entire list?我将如何使名称出现,然后一旦发送数据就更新列表但不更新整个列表? At the moment, it allows it to update and change, and no issues if it's 1 item being updated but if there are 2 or more it updates too fast to see.目前,它允许它更新和更改,如果它是 1 个正在更新的项目没有问题,但如果有 2 个或更多它更新太快而无法看到。 How do I get around this?我该如何解决这个问题?

I have fixed this by updating an array of objects on the server-side.我已经通过更新服务器端的对象数组来解决这个问题。 Updating a service on that list and returning the entire list.更新该列表上的服务并返回整个列表。 This tackled the issue of having it update too fast.这解决了它更新太快的问题。

End code front end code:结束代码前端代码:

export default class extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            services: []
        }
        
        getAppData((err,opt, data) => {
            if (opt == "sysinfo"){
                this.setState({services: data})
            }
        });
    }
    
    componentDidMount() {
        fetch("http://localhost:3000/api/v1/bot/me/getservices").then(res => res.json()).then(data =>{
            console.log(data)
            this.setState({services: data})
        })
        
    }
    render() {
        const totalList = this.state.services

        const listItems = totalList.map((link) =>
            <ListGroup.Item key={link.id}>Name: {link.name} Node: {link.node} <br/>Ram usage: {link.p_ram.toFixed(2)} / 100% Cpu usage: {link.p_cpu.toFixed(2)} / 100%</ListGroup.Item> 
        );

        return (
                <div>
                    <ListGroup>
                        {listItems}
                    </ListGroup>
                </div>
        );
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM