React.js life cycle componentdidupdate Alternative usage: Prevent page transitions from refreshing

Source: Internet
Author: User

Scenario: After the data has been added successfully, it needs to return the original query table, when the query needs to use the React life cycle: componentdidupdate

The Componentdidupdate () function of this life cycle is to update the DOM node with the props or state after it has been updated. If used improperly, the query page will continue to call the method of the query, do not stop the refresh operation. Therefore, it is necessary to add a flag to the new method, judging by this flag, if the new success, then call the Query method, otherwise, it is not called.

This flag, usually used in 2 locations:

1. New method of action;
2. Query the this.state of the component and componentdidupdate ();

These two flags are:
Addgroupresponseflag //New sign of Success
Addresponseflaghas:false //If the flag is updated after the new success, the default is False

The following code shows:

Action method, which is used before return
Let Addgroupresponseflag = httputils.httpresponseflag (params); return { type:add_version_group, data: { params, // New Success Flag addgroupresponseflag } }
//Query ComponentConstructor (props) {super (props);  This. State = {  //whether to update flag after new completion, default false      Addresponseflaghas:false    }//This life cycle needs to be called after the page updates the data componentdidupdatecomponentdidupdate () {/** * To get the data of the method to pass parameters, paging*/Let pageSize= This. state.pagesize; Let Pagenum= This. State.pagenum; /** * This is using ES6 object deconstruction assignment, API receive parameter only one variable, here assignment, just value {pagenum, pageSize}, the value of the passed object*/let addgroupresponseflag = This. Props.versionGroupState.addGroupResponseFlag; Let addresponseflaghas = This. State.addresponseflaghas; if(Addgroupresponseflag &&!)Addresponseflaghas) {       This. SetState ({ Addresponseflaghas:true  }); /** * Addgroupresponseflag &&!addresponseflaghas True, re-query method is executed*/   This. Props.searchappversiongroup ({pagenum, pageSize}); }  /** * If there is a deletion or modification, then write the judgment method below*/}

If there is a better way, welcome to communicate!

React.js life cycle componentdidupdate Alternative usage: Prevent page transitions from refreshing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.