robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.62k stars 135 forks source link

Why's `setMessageHistory` updater function used as dependency in the example code? #219

Closed Nisthar closed 8 months ago

Nisthar commented 1 year ago
const [messageHistory, setMessageHistory] = useState([]);

  const { sendMessage, lastMessage, readyState } = useWebSocket(socketUrl);

  useEffect(() => {
    if (lastMessage !== null) {
      setMessageHistory((prev) => prev.concat(lastMessage));
    }
  }, [lastMessage, setMessageHistory]);

What's the point of passing the setMessageHistory function as a dependency?

cornejobarraza commented 12 months ago

Linters usually scream at you if you don't pass setState as a dependency, that might be the reason he did it. React docs (only found this in legacy) says you can omit it as a dependency, so passing it shouldn't be an issue either:

https://legacy.reactjs.org/docs/hooks-reference.html#usestate

robtaussig commented 8 months ago

Fixed in b12af6d