在Reactjs上收听socket.io服务器时如何setState()?

发布于 2025-01-23 17:42:32 字数 1220 浏览 0 评论 0原文

我开发了一个socket.io服务器,该服务器每10秒将数据发送给客户端。 socket.io客户端用于我使用ReactJS类组件创建的应用程序。我连接到componentDidmount中的套接字,并在componentDidupdate中收听了套接字。

let socket = null
 
class Map extends Component {
    constructor(props) {
        super(props)
        this.state= {
            stateData: null
        }
    }
 
    componentDidMount() {
        socket = io.connect('http://localhost:8080');
        // calling some method
    }
 
    componentDidUpdate( {
        if(socket !== null) {
            socket.on("positionUpdates", (data) => {
                // console log to check if the data was connected successfully and the time when it got the data
                console.log("connected", data, new Date())
                this.setState({stateData: data})
            })
        }  
    }
 
    render() {
        return (
     
        )
    }
}

我将使用setState()将我从套接字中获得的数据保存到状态。但是在运行代码时,我遇到了一件奇怪的事情。我在socket.on之后做了console.logconsole.log应在检索数据时每10秒发生一次。但是发生的是console.log多次显示。当我删除setState时,代码通常会再次运行。

在socket.io client上收听socket.io服务器时,我应该如何做setState

I developed a socket.io server that sends data every 10 seconds to the client. Socket.io client is used in an application that I created with ReactJs class component. I connected to the socket in componentDidMount and listened for the socket in componentDidUpdate.

let socket = null
 
class Map extends Component {
    constructor(props) {
        super(props)
        this.state= {
            stateData: null
        }
    }
 
    componentDidMount() {
        socket = io.connect('http://localhost:8080');
        // calling some method
    }
 
    componentDidUpdate( {
        if(socket !== null) {
            socket.on("positionUpdates", (data) => {
                // console log to check if the data was connected successfully and the time when it got the data
                console.log("connected", data, new Date())
                this.setState({stateData: data})
            })
        }  
    }
 
    render() {
        return (
     
        )
    }
}

I will save the data that I get from the socket into a state using setState(). But I ran into a strange thing when running the code. I made a console.log after socket.on. console.log should happen once every 10 seconds when the data is retrieved. But what happens is that the console.log is displayed multiple times. When I remove the setState, the code normally runs again.

How should I do setState when listening to socket.io server on socket.io client?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2025-01-30 17:42:32

因此,从我对socket.io的侦听器上的理解中,您可以收听相同的事件,但具有不同的功能。

因此,您可以做类似的操作

socket.on("event1", () => console.log("this is my 1st listener"))
socket.on("event1", () => console.log("this is the 2nd listener but with same event name"))

,因此每次设置状态后componentDidupdate被触发并socket.on(“ positionupdates”,(data)=> {... {...})多次注册positionupdates事件。

因此,要修复此操作,将您的socket.on s移至componentdidmount,并确保在卸载后离开侦听器。

eventHandlerFunc(data) {
    // do something with data
},
componentDidMount() {
    socket = io.connect('http://localhost:8080');
    socket.on("event name", eventHandlerFunc);
},
componentWillUnmount() {
    socket.off("event name", eventHandlerFunc)     
}

So from my understanding of socket.io's on listener, is that you can listen the same event but with different functions.

So you can do something like

socket.on("event1", () => console.log("this is my 1st listener"))
socket.on("event1", () => console.log("this is the 2nd listener but with same event name"))

So after everytime you set state the componentDidUpdate gets fired and socket.on("positionUpdates", (data) => {...}) registers the positionUpdates event multiple times.

So to fix this move your socket.ons to componentDidMount and also make sure to off the listener after unmount.

eventHandlerFunc(data) {
    // do something with data
},
componentDidMount() {
    socket = io.connect('http://localhost:8080');
    socket.on("event name", eventHandlerFunc);
},
componentWillUnmount() {
    socket.off("event name", eventHandlerFunc)     
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文