Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
Why is websocket which is receoving periodic data causes rerender

Just by instantiating the following code in another components

import React, { useState, useCallback, useEffect } from 'react';
import useWebSocket, { ReadyState } from 'react-use-websocket';

export const WebsocketHandler = () => {
    const { lastJsonMessage, readyState } = useWebSocket('ws://127.0.0.1:8001/ws', { 
        shouldReconnect: (closeEvent) => {
        return true;
      }, 
      reconnectAttempts: 99999,
      reconnectInterval: 5000 
    });

  const [isResponsive, setIsResponive] = useState(true);

    useEffect(() => {
        console.log("lastJsonMessage: " + JSON.stringify(lastJsonMessage));
    }, [lastJsonMessage])

    
    useEffect(() => {
        console.log("readyState: " + readyState);
    }, [readyState])

  return isResponsive;
};

I got rerenders or to be more precise, i got some console output from my other components, so it looks like stuff is rerendered on every receive. I don't use the return value, so whats going on here? How can I avoid that? Edit: The console log in this compoment is not the issue. The output will also come from other compoments in the application.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

useEffect will run everytime lastJsonMessage or readyState value change. useEffect is a react hook and will run after side effects based parameters. For more info Using the Effect Hook

Data fetching, setting up a subscription, and manually changing the DOM in React components are all examples of side effects. Whether or not you’re used to calling these operations “side effects” (or just “effects”), you’ve likely performed them in your components before.

If you’re familiar with React class lifecycle methods, you can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined.

If you don't want to logging delete the both useEffects. if you want console log once

useEffect(() => {
    console.log("lastJsonMessage: " + JSON.stringify(lastJsonMessage));
    console.log("readyState: " + readyState);
}, [])

but this code may want to change based your want

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda