Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda