handling multiple ajax calls in reactjs

Imesh Chandrasiri

I've got the following code in my react component.

componentDidMount() {
    getOrganizationsForUserTemp().then((organizations) => {
        this.setState({
            userOrganizations: organizations,
        });
    });

    getUsersOfOrganizationTemp().then((users) => {
        this.setState({
            userOrganizations: users,
        });
    })
}

The above code initiates two ajax calls and when the response is retrieved, it updates the state of the component which results in a re-render of the component.

My problem is that, when the first response is received, the component renders perfectly and then when the second response comes, the previously rendered components disappear from the DOM. I guess that when the second response updates the state, there is no reference for the previous response state.

How can I manage multiple ajax calls in such a way that I could have multiple in a single component? what am I doing wrong here.

Hozefa

Since you are storing the response of both ajax calls into the same state variable you are facing this issue.

You need to do something like

componentDidMount() {
    getOrganizationsForUserTemp().then((organizations) => {
        this.setState({
            userOrganizations: organizations,
        });
    });

    getUsersOfOrganizationTemp().then((users) => {
        this.setState({
            users: users,
        });
    })
}

organizations & user are stored in separate state variables.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Handling multiple network calls in a single service

分類Dev

How to control pressure of multiple ajax calls with RxJS

分類Dev

Issues with multiple AJAX calls requesting JSONP

分類Dev

multiple ajax calls on button click if page is not refreshed jquery

分類Dev

Multiple AJAX calls return JSON objects in random order

分類Dev

Handling json error messages in blob http calls

分類Dev

Handling multiple errors in go

分類Dev

Handling multiple get operations

分類Dev

Handling multiple formats in DateTimeFormatter

分類Dev

iOS - Handling Multiple Checkboxes

分類Dev

Handling multiple forms, Spring

分類Dev

Chain of promises in javascript with ajax calls

分類Dev

Ajax calls to server are not getting triggered

分類Dev

Handling AJAX in nested loop (Angularjs)

分類Dev

Making parallel async await calls in ReactJS

分類Dev

Express middleware calls multiple times

分類Dev

Multiple calls to configure website not working

分類Dev

XhrIo making multiple calls onclick

分類Dev

Mongoose and multiple database error handling

分類Dev

Handling multiple dynamic datatype in JSON

分類Dev

Handling multiple submit button in form

分類Dev

Handling multiple heads after graft

分類Dev

Handling multiple fact tables in Qlikview

分類Dev

jQuery Ajax calls and the Html.AntiForgeryToken()

分類Dev

React - Controlling AJAX calls made to the server

分類Dev

DeveloperExceptionPage not showing through Ajax Calls in .NetCore 2.2

分類Dev

Emberjs a good way to handle async ajax calls

分類Dev

Yii: Javascript & CSS not loading after Ajax calls

分類Dev

How to make AJAX calls with Dynamic URLS in HDIV

Related 関連記事

ホットタグ

アーカイブ