New Sep 3, 2026

Why does React render an intermediate state between two setState calls? [duplicate]

Libraries, Frameworks, etc. All from Newest questions tagged reactjs - Stack Overflow View Why does React render an intermediate state between two setState calls? [duplicate] on stackoverflow.com

I have built an AddressSearch page but the behaviour is not as expected.

How does it work?

The issue is happening in this case →

  1. First, I search "California, US" and scroll down → I see three API calls as expected and 43 rows I see on UI.

  2. Then I search "Albama, US" and now handleAddressChange() gets called(as expected).
    But I see two API calls - with these payloads →

    {
        "searchBy": "address",
        "address": {
            "street": "",
            "postalCode": "",
            "city": "",
            "state": "AL",
            "country": "US",
            "formattedAddress": "Alabama, USA"
        },
        "page": 3,
        "size": 20,
        "sortBy": "id",
        "minimumStage": 4
    }
    

    and

    {
        "searchBy": "address",
        "address": {
            "street": "",
            "postalCode": "",
            "city": "",
            "state": "AL",
            "country": "US",
            "formattedAddress": "Alabama, USA"
        },
        "page": 1,
        "size": 20,
        "sortBy": "id",
        "minimumStage": 4
    }
    

    where as I only expect one call with the second payload.

  3. I'm totally confused on why the second call is happening. I did console log and this is what it showed
    HANDLE ADDRESS CHANGE {address: 'Alabama, USA', currentPage: 0}
    RENDER 34 {address: 'Alabama, USA', page: 2}
    QUERY INPUT {address: 'Alabama, USA', pageState: 2, apiPage: 3}
    RENDER 35 {address: 'Alabama, USA', page: 0}
    QUERY INPUT {address: 'Alabama, USA', pageState: 0, apiPage: 1}
    RENDER 36 {address: 'Alabama, USA', page: 0}

  4. I don't understand the behaviour → first log printed the updated address and page as 0. Then why again the next log printed page as 2?

  5. I understand handleAddressChange() is the reason behind this - the state updates in it.
    But I don't understand why this behaviour and how to deal with this. I've had multiple conversations with Opus and ChatGPT but couldn't make it work.

Scroll to top