Lifecycle
Mini program supports development using React, but it is slightly different from using React in the browser.
#
The entry componentEvery mini program application requires an entry component to register the application, and the default entry file is app.js in the src directory.
The entry component must export a React component. In the entry component we can set the global state or access the lifecycle of the entry instance: the
file: app.js
#
Entry configurationWe can add a new app.config.js file for global configuration, and the default export of app.config.js is the global configuration.
The configuration specification is based on the global configuration of the mini program.
file: app.config.js
#
onLaunch ()Trigger when the app is launched.
#
componentDidShow ()Triggered when the program is launched, or when the foreground is cut.
#
componentDidHide ()Triggered when the application cuts the background.
#
componentDidCatch(err, info)Listens for Mini Program error events. Such as a script error or API Call error, etc. The first parameter err points to the thrown error, and the second parameter info is the call information of the component.
#
componentDidCatchError(err)Listens for Mini Program error events. Such as a script error or API Call error, etc. Only use in APP. The parameter err points to the thrown error.
#
onPageNotFound (Object)Triggered when the page to be opened by the application does not exist.
#
Parameters#
ObjectProperty | Type | Description |
---|---|---|
path | string | Path to the page that does not exist |
query | Object | The query parameter to open a non-existent page |
isEntryPage | boolean | Whether it is the first page launched this time |
#
Page componentsEvery mini program includes at least one page component that can jump through the route and access the lifecycle of the mini program's page.
Class Component
Functional Component
#
onTabItemTapFired whenever tab item tapped
#
Parameters#
ObjectProperty | Type | Description |
---|---|---|
index | String | the index of the tab item starting from 0 |
pagePath | String | the path of the tab item |
text | String | the text value of the tab item |
#
Page ConfigurationFor each page file (e.g. . /pages/index/index.jsx), we can add a new . /pages/index/index.config.js file for page configuration, and the default export of index.config.js is the page configuration.
#
Routing parametersIn the page component, you can get the current page's routing parameters via getCurrentInstance().router.
#
Lifecycle triggering mechanism#
React's lifecycleThe lifecycle methods of React components are supported in mini program.
Trigger timing:
- componentWillMount ()
Triggered after onLoad, before the page component is rendered to BMP's virtual DOM.
- componentDidMount ()
Triggered after the page component is rendered to BMP's virtual DOM.
However, this does not mean that virtual DOM data has been transferred from the logical level setData to the view level. It can only be done during the onReady lifecycle.
Methods for mini program pages
The methods of the page can also be used: write the methods of the same name in the Class Component and use the corresponding Hooks in the Functional Component.
#
onLoad (options)Page routing parameters can be accessed during this lifecycle by calling getCurrentInstance().router.
#
onReady ()From this lifecycle mini program has already rendered DOM nodes to the rendering layer using APIs.
onReady for child components
The onReady lifecycle is only triggered for page components. Child components can listen to the onReady() lifecycle of a page component using mini program's built-in messaging mechanism.
A child component in a page
#
componentDidShow ()Triggered when the page is displayed/cut to the foreground.
onShow for child components
The onShow lifecycle is triggered only for page components. Child components can listen to the onShow() lifecycle of a page component using mini program's built-in messaging mechanism.
A child component in a page
#
componentDidHide ()Triggered when the page is hidden/cut to the background.
onHide for child components
The onHide lifecycle is triggered only for page components. Child components can listen to the onHide() lifecycle of the page component using mini program's built-in messaging mechanism.
A child component in a page
#
componentDidCatch(err, info)Listens for Mini Program error events. Such as a script error or API Call error, etc.
Parameter Object object:
Property | Type | Description |
---|---|---|
err | thrown error | |
info | the call information of the component |
A child component in a page
#
onPageScroll()Listens user swiping the page.
Parameter Object object:
Property | Type | Description |
---|---|---|
scrollTop | Number | Scroll distance in the vertical direction (unit: px) |
Note: To reduce the impact of triggering of nonessential events on communication between the rendering layer and the logic layer, this method should be defined in page only when necessary. Do not define an empty method. Note: Avoid repeated execution of in onPageScroll that may trigger communication between the rendering layer and the logic layer. Transferring large volume of data each time will result in longer communication time.
A child component in a page
#
onReachBottom()Listens user swiping up to the bottom of the page.
- You can set the trigger distance onReachBottomDistance in the app.json window option or in the Page Configuration.
- This event is only triggered once when the page scrolls within the trigger distance.
A child component in a page
#
onShareAppMessage(Object)Listen to the user click on the in-page share onbutton assembly open-type="share" Or the upper-right menu "Share" on behavior, or execute mpservice.showSharePanel
, and customize the share content.
#
Parameters#
ObjectProperty | Type | Description |
---|---|---|
from | String | Share the source of the event. button : In-page share button, menu : Retweet menu on top right, code : Execute mpservice.showSharePanel |
target | String | if from The value is button , Then target That triggered this retweet. button , otherwise undefined |
This event handler requires return One Object or Promise, used to customize the sharing content, returns the following:
field | Type | Introductions | Default value |
---|---|---|---|
title | String | Sharing Title | Current applet name |
desc | String | Description | |
imageUrl | String | Custom picture path, should be network picture path. Support for PNG and JPG. | Use default screenshots |
path | Function | Sharing path | Current page path Must be based on / Full path to start |
success | Function | Callback function when the user forwards on |