Answers for "socket.io react"

2

socket.io react

import React, { useState, useEffect } from "react";
import socketIOClient from "socket.io-client";
const ENDPOINT = "http://127.0.0.1:4001";

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>
  );
}

export default App;
Posted by: Guest on November-21-2020
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

socket io react js

npm install socket.io
Posted by: Guest on December-02-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language