sockjs/sockjs-client

React setState not updating state inside stompjs callback function

neerajjain92 opened this issue · 3 comments

const [stompClientRef, setStompClientRef] = useState(false); // ------> This is the state

const onConnected = () => {
    console.log(
      "Connected to backend StompEndpoint...!! ",
      WEB_SOCKET_BACKEND_URL
    );
    stompClient.subscribe(
      "/topic/live-tracking/1-neeraj/2-admin",
      handleOnMessageReceived,
      { id: "live-tracking-ws-topic-id" }
    );
  };

  const handleOnMessageReceived = (msg: any) => {
    console.log("StompClient..........", stompClient);
    setStompClientRef(true);
    console.log("stompClientRef.......", stompClientRef);
    const tripGeoCoordinate = JSON.parse(msg.body);
    console.log(tripGeoCoordinate);
  };

Even when handleOnMessageReceived is invoked, setState isn't working.... stompClientRef is always false.

same here the initial state itself is sustained by the states inside the callback function. when ever I tried to log them. If anyone could resolve this problem it would be nice.

I would love to have a PR with failing test first

This issue has been inactive for 30 days. It will be in closed in 5 days without any new activity.