React state contains correct data initially but is overwritten by previous value2019 Community Moderator ElectionCorrect modification of state arrays in ReactJSWhat is the difference between state and props in React?React js onClick can't pass value to methodjson data as react state is not updatedHow get value datapicker in react toobox custom?Update React Grandparent State from onClick in GrandchildReact fetch data getting lostReact Concat StatesetState also changes screen propsfetch data in react native from mlab

They call me Inspector Morse

How do I deal with a powergamer in a game full of beginners in a school club?

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?

Why does the negative sign arise in this thermodynamic relation?

The bar has been raised

In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?

Solving "Resistance between two nodes on a grid" problem in Mathematica

Do f-stop and exposure time perfectly cancel?

Regex for certain words causes Spaces

Why the color red for the Republican Party

What is the likely impact of grounding an entire aircraft series?

Is there any way to damage Intellect Devourer(s) when already within a creature's skull?

Word for a person who has no opinion about whether god exists

Good for you! in Russian

"One can do his homework in the library"

Is having access to past exams cheating and, if yes, could it be proven just by a good grade?

Is there an equal sign with wider gap?

Accountant/ lawyer will not return my call

Does splitting a potentially monolithic application into several smaller ones help prevent bugs?

Why is Beresheet doing a only a one-way trip?

Best approach to update all entries in a list that is paginated?

Are babies of evil humanoid species inherently evil?

If the Captain's screens are out, does he switch seats with the co-pilot?



React state contains correct data initially but is overwritten by previous value



2019 Community Moderator ElectionCorrect modification of state arrays in ReactJSWhat is the difference between state and props in React?React js onClick can't pass value to methodjson data as react state is not updatedHow get value datapicker in react toobox custom?Update React Grandparent State from onClick in GrandchildReact fetch data getting lostReact Concat StatesetState also changes screen propsfetch data in react native from mlab










-1















i have a state variable which is initially set to null. After getting data from server i update its state. In doing so.. it loads correct data for items with different ids. however on opening the item that was previously opened... sets the state with correct value. however it then overwrites the state value with previous opened item.



I am not sure what going wrong.



Below is the code,



class Items extends React.PureComponent  











-1















i have a state variable which is initially set to null. After getting data from server i update its state. In doing so.. it loads correct data for items with different ids. however on opening the item that was previously opened... sets the state with correct value. however it then overwrites the state value with previous opened item.



I am not sure what going wrong.



Below is the code,



class Items extends React.PureComponent improve this answer

























  • its a typo...although i mean the state value is overwritten by previous value.

    – stackoverflow_user
    Mar 6 at 16:23









share


Note that this code will not react to any changes to item_id while load_item_details is fetching data. And it will break on error inside client.get_item_details_file.






share|improve this answer

























    0












    0








    0







    You have item_id inside method 1 and item_id inside method 8 in the second log. Looks like you send two simultaneous requests with different ids. Since data for item_id=1 is much larger than data for item_id=8 it arrives later and overwrites everything. Try to call load_item_details only when needed and use some flag to block simultaneous requests.



    ...
    this.state =
    item_details: null,
    isLoading: false,
    ;
    ...
    componentDidUpdate(PrevProps)
    if (PrevProps.item_id !== this.props.item_id) this.load_item_details();

    ...
    load_item_details = () =>
    if (this.state.isLoading) return;
    const file_name = 'item_details.json';
    this.setState(isLoading: true);
    client.get_item_details_file(this.props.item_id, file_name, 'json')
    .then((request) =>
    this.setState(item_details: request.response, isLoading: false);
    );
    }


    Note that this code will not react to any changes to item_id while load_item_details is fetching data. And it will break on error inside client.get_item_details_file.






    share|improve this answer













    You have item_id inside method 1 and item_id inside method 8 in the second log. Looks like you send two simultaneous requests with different ids. Since data for item_id=1 is much larger than data for item_id=8 it arrives later and overwrites everything. Try to call load_item_details only when needed and use some flag to block simultaneous requests.



    ...
    this.state =
    item_details: null,
    isLoading: false,
    ;
    ...
    componentDidUpdate(PrevProps)
    if (PrevProps.item_id !== this.props.item_id) this.load_item_details();

    ...
    load_item_details = () =>
    if (this.state.isLoading) return;
    const file_name = 'item_details.json';
    this.setState(isLoading: true);
    client.get_item_details_file(this.props.item_id, file_name, 'json')
    .then((request) =>
    this.setState(item_details: request.response, isLoading: false);
    );
    }


    Note that this code will not react to any changes to item_id while load_item_details is fetching data. And it will break on error inside client.get_item_details_file.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 6 at 18:28









    UjinT34UjinT34

    78411




    78411























        0














        You probably have a typo on your code:



         load_item_details = () => 
        const file_name = 'item_details.json';
        client.get_item_details_file(this.props.item_id, file_name, 'json')
        .then((request) =>
        this.setState(item_detail: request.response);
        );
        }


        It should be this.setState(item_details: request.response)






        share|improve this answer

























        • its a typo...although i mean the state value is overwritten by previous value.

          – stackoverflow_user
          Mar 6 at 16:23















        0














        You probably have a typo on your code:



         load_item_details = () => 
        const file_name = 'item_details.json';
        client.get_item_details_file(this.props.item_id, file_name, 'json')
        .then((request) =>
        this.setState(item_detail: request.response);
        );
        }


        It should be this.setState(item_details: request.response)






        share|improve this answer

























        • its a typo...although i mean the state value is overwritten by previous value.

          – stackoverflow_user
          Mar 6 at 16:23













        0












        0








        0







        You probably have a typo on your code:



         load_item_details = () => 
        const file_name = 'item_details.json';
        client.get_item_details_file(this.props.item_id, file_name, 'json')
        .then((request) =>
        this.setState(item_detail: request.response);
        );
        }


        It should be this.setState(item_details: request.response)






        share|improve this answer















        You probably have a typo on your code:



         load_item_details = () => 
        const file_name = 'item_details.json';
        client.get_item_details_file(this.props.item_id, file_name, 'json')
        .then((request) =>
        this.setState(item_detail: request.response);
        );
        }


        It should be this.setState(item_details: request.response)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 6 at 16:19









        kiranvj

        12.9k23453




        12.9k23453










        answered Mar 6 at 16:18









        LuisMendes535LuisMendes535

        10317




        10317












        • its a typo...although i mean the state value is overwritten by previous value.

          – stackoverflow_user
          Mar 6 at 16:23

















        • its a typo...although i mean the state value is overwritten by previous value.

          – stackoverflow_user
          Mar 6 at 16:23
















        its a typo...although i mean the state value is overwritten by previous value.

        – stackoverflow_user
        Mar 6 at 16:23





        its a typo...although i mean the state value is overwritten by previous value.

        – stackoverflow_user
        Mar 6 at 16:23

















        draft saved

        draft discarded
















































        Thanks for contributing an answer to Stack Overflow!


        • Please be sure to answer the question. Provide details and share your research!

        But avoid


        • Asking for help, clarification, or responding to other answers.

        • Making statements based on opinion; back them up with references or personal experience.

        To learn more, see our tips on writing great answers.




        draft saved


        draft discarded














        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027591%2freact-state-contains-correct-data-initially-but-is-overwritten-by-previous-value%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown





















































        Required, but never shown














        Required, but never shown












        Required, but never shown







        Required, but never shown

































        Required, but never shown














        Required, but never shown












        Required, but never shown







        Required, but never shown







        Popular posts from this blog

        AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

        Алба-Юлія

        Захаров Федір Захарович