Answers for "how to connect socket in react js"

3

how react with socket

connect to the socket server on component mount with useEffect
save each new incoming message in the component's state.

function App() {
  const [response, setResponse] = useState("");

  useEffect(() => {
    const socket = socketIOClient(ENDPOINT);
    socket.on("FromAPI", data => {
      setResponse(data);
    });
  }, []);

  return (
    <p>
      It's <time dateTime={response}>{response}</time>
    </p>
  );
}
Posted by: Guest on November-21-2020
0

how to connect socket in react js

npm i socket.io-client
Posted by: Guest on April-13-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language