0

The useEffect would run more than once for some reason (usually twice) and would print my message twice (or more). I have tried multiple solutions even with useMontainEffect but the result is always the same. Any solutions?

import './App.css';
import io from 'socket.io-client'
import { useEffect, useRef, useState } from 'react'
import React from 'react';
import ReactDOM from "react-dom/client";


const socket = io.connect("http://localhost:3001");

function App() {
  const [message, setMessage] = useState("");
  const [state, setState] = useState([]);
  const [chat, setChat] = useState([]);
  const socketRef = useRef();
  const sendMessage = () => {
    socket.emit("send_message", { message });
  };

  const renderChat = () => {
    return (
        chat.map(msg => {
            return (
                <h3>{msg.message['message']}</h3>
            )
        })
    )
}


useEffect(() => {
  socket.on("receive_message", message => {
    setChat(prevState => [...prevState, {message}]);
  });
}, [socket])


  return (
    <div className="App">
      <input placeholder="Message..." onChange={(event) => {
        setMessage(event.target.value);}}
        />
      <button onClick={sendMessage}>Send Message</button>
      <h1>Message:</h1>
      {renderChat()}
    </div>
  );
}

export default App;

3

2 Answers 2

1

In strict mode the component will be mounted, unmounted, then re-mounted. You can add a cleanup function. The double invocation however is expected.

useEffect(() => {
  const listener = message => {
    setChat(prevState => [...prevState, {message}]);
  };
  socket.on("receive_message", listener);
  return () => socket.off('receive_message', listener);
}, [socket])

Sign up to request clarification or add additional context in comments.

1 Comment

Aziz's answer is correct. To complete it, you can visit stackoverflow.com/a/72238236/15288641.
-1

If u want to check, U can turn off the strict mode and check exactly how many times the useEffect call back function runs

Note : but don't always turn off the strict mode because

--> strict mode is actually because if we forgot any cleanup function in useEffect , ui behaves differently

--> with that only we came to know that we did something mistake

--> strict mode is present in only development mode, it won't be in production mode

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.