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

204
Visualizações
How to setState false when a component rendered? - RN

I am new to application development. As seen below when "value" is false, I want the "Test" component to be rendered and the loading state to be false. But when I do as in the codes below, I get the error in the picture below. How do I set state to false without getting an error when this test component is rendered on the page?

enter image description here

import React, { useState } from 'react'
import { Text, View } from 'react-native'


const WordPage = () => {

    let value = false

    const [loading, setLoading] = useState(false)

    const Test = () => {
        setLoading(false)
        return (
            <Text>OFFLINE</Text>
        )
    }

    return (
        <View>
            {value ?
                <Text>ONLINE</Text>
                :
                <Test />
            }
        </View>
    )
}

export default WordPage

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

0

You should be using the useEffect hook for this. useEffect is used for running side effects and runs after the component has rendered.

Also, you don't need this additional component. You can simply use the useEffect hook in your parent component.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can pass a callback as prop and call it once Test component is rendered.

import React, { useEffect, useState } from "react";
import { Text, View } from 'react-native'

const Test = ({ onRendered }) => {
  useEffect(onRendered, []);
  return <Text>OFFLINE</Text>;
};

const WordPage = () => {
  let value = false;
  const [loading, setLoading] = useState(false);

  return (
    <View>
      {value ? (
        <Text>ONLINE</Text>
      ) : (
        <Test
          onRendered={() => {
            setLoading(false);
          }}
        />
      )}
    </div>
  );
};

export default WordPage;
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