使用socket.io事件更新状态

问题描述 投票:0回答:1

我有一个react功能组件:

function Chat(props) {
  const [messages, setMessages] = useState([]);

  const inputRef = useRef();

  //The socket is a module that exports the actual socket.io socket
  socket.socket.on("chatMessage", (msg) => {
    setMessages([...messages, msg]);
  });

  const inputChanged = (e) => {
    if (e.key === "Enter") {
      socket.sendMessage(inputRef.current.value)
        .then(() => {
          //do something
        })
        .catch(console.log);
      inputRef.current.value = "";
    }
  };

  return (
      <div>
        {messages.map((msg, i) => <ChatMessage key={i}>{msg}</ChatMessage>)}
        <input ref={inputRef} onKeyPress={inputChanged} />
     </div>
  );
}

但是当我从socket.socket.on("chatMessage"更新状态时,出现错误

无法在已卸载的组件上执行React状态更新

套接字告诉我,它花了很长时间才能响应,并且开始出现一些递归。

我应该如何通过套接字事件更新组件状态?

javascript reactjs socket.io react-functional-component
1个回答
0
投票

您需要在useEffect函数中设置套接字列表器,否则在每次重新渲染时都会创建一个新实例,而旧实例将继续侦听并导致内存溢出和意外的状态更新错误。也请清除您的套接字列表器]

function Chat(props) {
  const [messages, setMessages] = useState([]);

  const inputRef = useRef();

  useEffect(() => {
      //The socket is a module that exports the actual socket.io socket
      const addMessage = (msg) => setMessages(prevMessages => [...prevMessages, msg]);
      socket.socket.on("chatMessage", addMessage)
      () => {
            // turning of socket listner on unmount
          socket.off('chatMessage', addMessage);
       }
  }, [])

  const inputChanged = (e) => {
    if (e.key === "Enter") {
      socket.sendMessage(inputRef.current.value)
        .then(() => {
          //do something
        })
        .catch(console.log);
      inputRef.current.value = "";
    }
  };

  return (
      <div>
        {messages.map((msg, i) => <ChatMessage key={i}>{msg}</ChatMessage>)}
        <input ref={inputRef} onKeyPress={inputChanged} />
     </div>
  );
}

// P.S.确保您正在使用回调方法来更新状态

© www.soinside.com 2019 - 2024. All rights reserved.