8 Lifecycle functions

This chapters covers

  • onMount to run code when a component is added to the DOM
  • beforeUpdate to run code before every component update
  • afterUpdate to run code after every component update
  • onDestroy to run code when a component is removed from the DOM

In some applications there are actions that need to be performed when a component is added to or removed from the DOM. There are also situations where actions need to be performed before or after a component is updated. Svelte supports this by allowing the registration of functions to be invoked when four specific events occur in the lifecycle of a component instance:

  • When it is mounted (added to the DOM)

  • Before it is updated

  • After it is updated

  • When it is destroyed ...

Get Svelte and Sapper in Action now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.