The life cycle and callback functions of Android React native components

Source: Internet
Author: User

Kids who are familiar with Android should be aware that Android has a life cycle, and that many of its components also have a life cycle. Today's small series and everyone to share the life cycle of the react native components, still do not know the children's shoes, hurry to onlookers it

In Android development , the life cycle of the React native component is broadly divided into three stages, namely:

1, the first drawing phase of the component, this phase is mainly the loading and initialization of components;

2, components in the operation and interaction phase, this phase of the component can handle user interaction, or receive the event update interface;

3, the component unloading the extinction stage, this stage is mainly the component cleanup work.

There are 10 callback functions in the entire life cycle of the Android React native component, and must not be known.

1, Object Getdefaultprops () is called once when the component class is created, and the return value is cached.

2, Object Getinitialstate () is called once before the component is mounted. The return value will be the initial value of the this.state.

3. Componentwillmount () is called immediately before the initialization of the render execution.

4, Reactcomponent render () This method is necessary, rendering the view, you can also return null or FALSE to indicate that there is no need to render anything.

5, Componentdidmount () is called immediately after the initialization of the render execution.

6, Componentwillreceiveprops (object nextprops) is called when the component receives a new props. The method is not called when the rendering is initialized. Can be used to update state to respond to a change in a prop.

7. Boolean Shouldcomponentupdate (Object Nextprops, Object nextstate) is called before it receives a new props or state, and if it is determined that the new props and state will not Causes the component to update, it should return false here. Returns true to render.

8, Componentwillupdate (object Nextprops, Object nextstate) is called when it receives a new props or state and Shouldcomponentupdate returns TRUE.

9. Componentdidupdate (Object Prevprops, Object prevstate) is called immediately after the component's update has been synchronized to the DOM.

10, Componentwillunmount () is called immediately when the component is removed from the DOM. Perform any necessary cleanup in the method, such as an invalid timer, or clear the DOM element created in Componentdidmount.

In order to deepen our knowledge and understanding, let's look at the test code together:

/**

* Sample React Native App

* http://www.maiziedu.com

*/

' Use strict ';

var React = require (' react-native ');

var {

Appregistry,

StyleSheet,

View,

} = React;

var awesomeproject = React.createclass ({

Called once when the component class is created, and then the return value is cached.

Getdefaultprops:function () {

Console.log ("Getdefaultprops");

return null;

},

The initialization state, called once before the component is mounted. The return value will be the initial value of the this.state.

Getinitialstate:function () {

Console.log ("Getinitialstate");

return null;

Must have a return value, which can be null or an object.

},

Component will be rendered

Componentwillmount:function () {

Console.log ("Componentwillmount");

},

Render View

Render:function () {

Console.log ("Render");

Return (

<View>

</View>

);

You can also return null or FALSE to indicate that you don't need to render anything.

},

After rendering the view is complete

Componentdidmount:function () {

Console.log ("Componentdidmount");

This.loaddatatosetstate ();

},

The component receives a new property, which is not called when the rendering is initialized.

Componentwillreceiveprops:function (nextprops) {

Console.log ("Componentwillreceiveprops");

Console.log (Nextprops);

},

Whether the component needs to be updated

Shouldcomponentupdate:function (nextprops,nextstate) {

Console.log ("Shouldcomponentupdate");

Console.log (nextprops+ "|" +nextstate);

return true;

},

Component will be updated

Componentwillupdate:function (nextprops,nextstate) {

Console.log ("Componentwillupdate");

Console.log (nextprops+ "|" +nextstate);

},

Component Update complete

Componentdidupdate:function (prevprops,prevstate) {

Console.log ("Conponentdidupdate");

Console.log (prevprops+ "|" +prevstate);

},

Before the component is destroyed, do cleanup operations

Componentwillunmount:function () {

Console.log ("Componentwillunmount");

},

Loaddatatosetstate:function () {

Console.log ("Loaddatatosetstate");

This.setstate ({

Name: "RN"

})

},

});

var styles = Stylesheet.create ({

});

Appregistry.registercomponent (' Awesomeproject ', () = Awesomeproject);

The final output is as follows, and we call the Loaddatatosetstate function in the Componentdidmount function, which is set by the SetState function, and the callback Shouldcomponentupdate , if true, continues to invoke Componentwillupdate, Render, Conponentdidupdate, and then press the back key to exit the app, the destroy operation, callback

Componentwillunmount

Getdefaultprops

Getinitialstate

Componentwillmount

Render

Componentdidmount

Loaddatatosetstate

Shouldcomponentupdate

Componentwillupdate

Render

Conponentdidupdate

Componentwillunmount

The trigger on the Componentwillreceiveprops function is triggered after the props property is changed, and I try to modify this property in the code, but without the result, it will be an error, and I'll ignore it. Just understand when the call is good.

And on the commissioning, React-native official website provides a chrome under debugging plug-in, see Chrome Developer Tools, after installing the plugin, on the phone developer options to open Debug JS, You can see the console output and JS error messages in Chrome.

The above is the life cycle of Android React native components and the use of related functions, we hope to learn about the relevant knowledge to help.

Related article: "Android Development tools commonly used shortcut keys Daquan"

The life cycle and callback functions of Android React native components

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.