I have been thinking about writing something to record my growth history, and I finally started to practice writing blog for the first time

preface

(1) Why is wechat small program

I just graduated this year, and I have been working as an intern in the company for nearly a year. During this period, I have been writing related functions of small programs, and I am familiar with some basic functions of small programs.

(2) Technical native JS + Redux + official API of wechat small program

Today is mainly about their understanding of the micro channel small program life cycle, although has been able to query the relevant documents on various platforms, but feel that write it out, can sort out my thoughts, but also hope to get the advice of danniu.

parsing

Applets are divided into two life cycles: global life cycle + page life cycle Applets App life cycle

App life cycle is mainly called in app.js, mainly including:

  • OnLaunch – Applet initialization complete, global trigger only once
  • OnShow – when the applets start, or from the background into the foreground
  • OnHide – Executed when the applets enter the background from the foreground
  • OnError – The applet is executed with an error message if the script fails to run or the API calls fail
  • OnPageNotFound — executed when the applet page does not exist

    Execution order: onLaunch–> onShow –> onHide

  • Pages life cycle of applets

    Pages life cycle mainly refers to the life cycle of JS corresponding to each file, mainly including:

  • OnLoad — executed once when the page loads
  • OnReady — executed once when the page is first rendered
  • OnShow — executed when the page is displayed, multiple times
  • OnHide — executed when the page enters the background from the foreground
  • OnUnload — Performed when a page is unloaded
  • Note: It is recommended to use onShow when switching pages requires multiple renderings of data to change state. It can be used in onLoad or onReady when only one initialization is required. You can use onUnload when you need to clear timers

    Execution order: onLoad –> onShow –> onReady –> onHide

    Life cycle triggered when switching pages: When page A is loaded for the first time, the life cycle triggered by page A is: OnLoad –> onShow –> onReady: When switching from page A to page B, page A triggers onHide. Page B triggers the same life cycle sequence as above. When page B returns to page A, onUnload is triggered.

    Pages instantiates the life cycle

    Small program official document has a chart that instantiation of the process, I will directly bring them here, can see developers.weixin.qq.com/miniprogram…

    Explanation: As shown below

    Operation mechanism

    Concept:

    Thermal activation: refers to the small program after the success of the start, you click the x in the upper left corner or press the home button to leave small programs, small program did not directly be destroyed, but into the mechanism of the background, when you open the small program again in a certain time, the small program at this time back into the front desk, from the background to render a page, this process is the hot start

    Cold start: This is when the mini program is first loaded (never opened), or when you uninstall the mini program, or when it is automatically destroyed by wechat, and when you reload the mini program again, this process is called cold start

    Note: Applets only trigger the onLaunch life cycle if they are cold launched

    conclusion

    The life of a small program:

  • Open the small program :(App)onLaunch –> (App)onShow –> (Pages)onLoad –> (Pages)onShow –> (Pages) onReady
  • Go to Next page :(Pages)onHide –> (Next)onLoad –> (Next)onShow –> (Next)onReady
  • Back to previous page :(curr)onUnload –> (pre)onShow
  • Leave the small program :(App)onHide
  • Enter again: applets not destroyed –> (App)onShow(execute the order above), applets destroyed, (App) onLaunch restart execution.