0

I have a form that want to send message via webSocket when click on submit button but after click and send message my component repeats in a cycle and my console shows a lot of console.log; I tried everything and only send message caused that; what can I do?

const [ISINData, setISINData] = useState<ISINDataType[]>([]);

  const { sendJsonMessage, lastJsonMessage, readyState, getWebSocket } =
    useWebSocket("ws://localhost:8425/", {
      shouldReconnect: (closeEvent) => true,
      reconnectInterval: 100,
    });


const onSubmitClick = useCallback(() => {
    sendJsonMessage({ subscribe: "DE000BASF111" });
  }, [])

 return (
    <div className="App h-full container">
      <span>The WebSocket is currently {connectionStatus}</span>
      <div className="flex w-full h-full flex-col justify-center">
        <div className="flex items-center p-4 box-border-sizing justify-center">
          <ISINForm onSubmit={onSubmitClick} className="flex items-center" />
        </div>
        <Card
          classNames={classNameCreator([
            "flex-grow-1 pr-4 pl-4 box-border-sizing",
            styles.content,
          ])}
        >
          <ul className={classNameCreator([styles["watch-list"], "p-0 m-0"])}>
            {ISINData?.map((ISIN) => (
              <ISINCard
                ISINData={ISIN}
                key={ISIN?.isin}
                onClick={() => onRowClick(ISIN?.isin)}
              />
            ))}
          </ul>
        </Card>
      </div>
    </div>
  );

N.SH
  • 616
  • 7
  • 20
  • 1
    With this snippet, I can only confirm that some other method is calling sendJsonMessage or onSubmitClick in a loop, but it is not possible to perform an analysis. – EvoluWil May 03 '23 at 20:58
  • @EmielZuurbier I complete the snippet – N.SH May 03 '23 at 21:27
  • @WillianGit I completed the snippet – N.SH May 03 '23 at 21:28
  • There's nothing obvious that could cause it. Are you setting a state somewhere else with the received JSON data from the WebSocket? – Emiel Zuurbier May 03 '23 at 21:35
  • no only sending message causes the re-render @EmielZuurbier – N.SH May 03 '23 at 21:36
  • I can't see how that is the case: sending a message shouldn't update a state and trigger a re-render, but receiving data and updating states in the `useWebSocket` hook should. If you claim that `onSubmitClick` keeps getting called, then the culprit might lie in the `ISINForm` component, which is the only component which could call that function, based on the information you've presented. – Emiel Zuurbier May 03 '23 at 21:55

0 Answers0