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

115
Views
React too many rending with useState

My Code :

import Item from './Item_att.js';
import axios from 'axios';

const Attraction_list = () => {
...
const [maindata, setMainData] = useState([]); //setData
const [postsPerPage] = useState(6);
...

let attList = async (areaCode, cityCode) => { //approach openAPI
    const url = `...`;
    try {
        const {data: res} = await axios.get(url)
        const list = res.response.body.items.item;

        console.log("rendering!")
        setMainData(list);
    } catch (err) {
        console.log(err);
    }
}

...

attList(1, 1).then(data => {console.log("then");}) //make maindata
const currentPosts = (tmp) => {    //slice data
    return tmp.slice(indexOfFirst, indexOfLast);
}

...

return (
    <div>
        ...
        <Item rlist={currentPosts(maindata)} moveTo={moveTo} area={area} city={"강남"}></Item>
    </div>
);
};

export default Attraction_list;

Function attList brings data from openAPI. Function currentPosts is executed when component Item is rendering, it returns a sliced list.

I think they executed only once,

Result : Too much rerendering on this program. Function attList and function currentPost executed too many times. I don't know why.

Why rerendering so much?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You should call the function attList inside a useEffect hook, else it will be called on each rerender of the component.

React.useEffect(()=>{

    attList(1, 1)

},[])
about 4 years ago · Juan Pablo Isaza Report

0

When you call

setMainData(list);

inside your attList function, react will rerender your component as there is a state change. This will then run your attList function again which is what is causing the loop.

To prevent this behaviour you can use the useEffect react hook to call the attList function once on the first render and only on the first render.

https://reactjs.org/docs/hooks-effect.html

about 4 years ago · Juan Pablo Isaza Report

0

You have made an infinity loop because the attList will execute setMainData which will then rerender the component again and therefor execute the attList again.

This is why you should use the useEffect hook, to make sure that the attList is only executed when you wan't it to. If you do as shown above, then it will only happen when the component is mounted.

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!