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

215
Visualizações
prevent re-rendering when multiple component called from a single parent component

I have multiple component which has state change during its data collection from get api call, are called from a single component get re-render multiple times , Please help to avoid re-render when open this page

const MyData = () => {
  const [data, setData] = useState("");
  const [newData, setNewData] = useState("");
  const getData = () => {
    axios.get("url").then(async function (response) {
      setData(response);
    });
  };
  const getData2 = () => {
    axios.get("url").then(async function (response) {
      setNewData(response);
    });
  };
  useEffect(() => {
    getData();
    getData2();
  });
  const NewData = () => {
    return (
      <View>
        <Text>{data.name}</Text>
      </View>
    );
  };
  const RewData = () => {
    return (
      <View>
        <Text>{newData.name}</Text>
      </View>
    );
  };
  return (
    <View>
      <NewData />
      <RewData />
    </View>
  );
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You provide no dependency array (undefined) to your useEffect. If the component's state changes, then the useEffect will be called again, which sets the state again, and so on.

You can provide an empty dependency array which will cause the useEffect to be called only once.

useEffect(() => {
    getData();
    getData2();
}, []);

Edit: In response to the comments. You can prevent the screens content from rendering until the data has been fetched as follows.

const MyData = () => {
  const [data, setData] = useState();
  const [newData, setNewData] = useState();

  const getData = () => {
    axios.get("url").then(async function (response) {
      setData(response);
    });
  };

  const getData2 = () => {
    axios.get("url").then(async function (response) {
      setNewData(response);
    });
  };

  useEffect(() => {
    getData();
    getData2();
  }, []);
  
  if (!data || !newData) {
     return null
  }
   
  const NewData = () => {
    return (
      <View>
        <Text>{data.name}</Text>
      </View>
    );
  };

  const RewData = () => {
    return (
      <View>
        <Text>{newData.name}</Text>
      </View>
    );
  };

  return (
    <View>
      <NewData />
      <RewData />
    </View>
  );
};
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