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

129
Visualizações
Display Not Updating when using UseEffect()

I am trying to render an array of IP Addresses, after pushing each IP to an empty array. Once the array has been populated, I am able to see it correctly in the console, but for some reason, it refuses to show up on screen despite mapping it.

Variables:

var ipList = [];
let key = 0;
const [subnet, setSubnet]  = useState("");

This is the first useEffect which is supposed to trigger immediately when the component mounts. This is working fine.

useEffect(() =>  {
    const callNativeCode = async () => {
      await ConnectedDevices.displayConnectedDevices( (arrayResponse) => {setArray(arrayResponse), console.log("FIRST NATIVE CALL" + arr)}, (error) => {console.log(error)}  );
  }
  callNativeCode();
  },[]) 

This is the second useEffect which is supposed to populate the empty array with pingable IPs as soon as we get the subnet from the native java code. Also working fine and I get the array as I need it to be.

useEffect(() => {
      const ipLoop = async () => {

      if( subnet != "")
      {
        for(let i = 1; i<=254; i++)
        {
          let ipAddress = subnet + "." + i;
          try{
            const ip = await Ping.start(ipAddress, {timeout: 100});  
            const { receivedNetworkSpeed,sendNetworkSpeed,receivedNetworkTotal,sendNetworkTotal } = await Ping.getTrafficStats(ipAddress, {timeout:1000});
            console.log(ipAddress + " Network Stats: " + receivedNetworkSpeed,sendNetworkSpeed,receivedNetworkTotal,sendNetworkTotal);
            key = i;
            ipList.push(key , ipAddress);
          }
          catch(error) 
          {
            console.log(i + " Error Code:  " + error.code + " ," + error.message)
          }
        }
        renderIPList();
      }
    }
    ipLoop();

  },[subnet]);

And this is the render function that is supposed to display the array on screen. The log is working fine and I can see the array as intended. It just refuses to render.

  const renderIPList = () => {
    console.log("RENDERING IPS: ", ipList);
    return ipList.map((items,index) => {
      return (
        <View key={index}>
          <Text>{items}</Text>
        </View>
      );    
    });
  };

Thank you in advance for your help.

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

0

Change your ipList to be derived from useState like this:

const [ipList, setIpList] = useState([]);

and use the setIpList in your useEffect

and don't call renderIpList() as a function from useEffect that should be in the component return statement.

So, the useEffect should update the state, and then the component renders the state.

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