Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!