Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Real-World Svelte

You're reading from   Real-World Svelte Supercharge your apps with Svelte 4 by mastering advanced web development concepts

Arrow left icon
Product type Paperback
Published in Dec 2023
Publisher Packt
ISBN-13 9781804616031
Length 282 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Tan Li Hau Tan Li Hau
Author Profile Icon Tan Li Hau
Tan Li Hau
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Part 1: Writing Svelte Components
2. Chapter 1: Lifecycles in Svelte FREE CHAPTER 3. Chapter 2: Implementing Styling and Theming 4. Chapter 3: Managing Props and State 5. Chapter 4: Composing Components 6. Part 2: Actions
7. Chapter 5: Custom Events with Actions 8. Chapter 6: Integrating Libraries with Actions 9. Chapter 7: Progressive Enhancement with Actions 10. Part 3: Context and Stores
11. Chapter 8: Context versus Stores 12. Chapter 9: Implementing Custom Stores 13. Chapter 10: State Management with Svelte Stores 14. Chapter 11: Renderless Components 15. Chapter 12: Stores and Animations 16. Part 4: Transitions
17. Chapter 13: Using Transitions 18. Chapter 14: Exploring Custom Transitions 19. Chapter 15: Accessibility with Transitions 20. Index 21. Other Books You May Enjoy

Coordinating lifecycle functions across components

As we reuse the same function across components, we can keep track globally of the components that use the same lifecycle function.

Let me show you an example. Here, I would like to keep track of how many components on the screen are using our lifecycle function.

To count the number of components, we can define a module-level variable and update it within our lifecycle function:

import { onMount, onDestroy } from 'svelte';
import { writable } from 'svelte/store';
let counter = writable(0);
export function setupGlobalCounter() {
  onMount(() => counter.update($counter => $counter + 1));
  onDestroy(() => counter.update($counter => $counter - 1));
  return counter;
}

As the counter variable is declared outside the setupGlobalCounter function, the same counter variable instance is used and shared across all the components.

When any component is mounted, it will increment the counter, and any component that is referring to the counter will get updated with the latest counter value.

This pattern is extremely useful when you want to set up a shared communication channel between components and tear it down in onDestroy when the component is being destroyed.

Let’s try to use this technique in our next exercise.

Exercise 2 – Scroll blocker

Usually, when you add a pop-up component onto the screen, you want the document to not be scrollable so that the user focuses on the popup and only scrolls within the popup.

This can be done by setting the overflow CSS property of the body to "hidden".

Write a reusable function used by pop-up components that disables scrolling when the pop-up component is mounted. Restore the initial overflow property value when the pop-up component is destroyed.

Do note that it is possible to have more than one pop-up component mounted on the screen at once, so you should only restore the overflow property value when all the popups are destroyed.

You can check the answer at https://github.com/PacktPublishing/Real-World-Svelte/tree/main/Chapter01/02-scroll-blocker.

You have been reading a chapter from
Real-World Svelte
Published in: Dec 2023
Publisher: Packt
ISBN-13: 9781804616031
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime