Reactive Classes with Getters and Setters in Svelte
Introduction to Reactive Classes in Svelte
In the previous lesson, you learned how to use $effect to implement debouncing, a technique that helps manage frequent updates efficiently. Now, we’ll shift our focus to organizing state logic using reactive classes in Svelte. While $state and $effect are powerful tools for managing reactivity, classes with getters and setters can help you structure your code more cleanly, especially when dealing with complex state logic.
In Svelte, reactivity is achieved through runes like $state, which allow you to create reactive variables. However, as your application grows, managing these variables individually can become cumbersome. Classes provide a way to encapsulate related state and logic into a single unit, making your code easier to maintain and understand.
In this lesson, you’ll learn how to use classes with getters and setters to manage reactive state in Svelte. By the end, you’ll be able to build a Temperature Converter that converts between Celsius and Fahrenheit using a reactive class. This builds on your knowledge of $state and introduces a new way to structure your applications.
Let’s get started!
Understanding Getters and Setters
Before diving into reactive classes, let’s briefly review what getters and setters are in JavaScript. Getters and setters are special methods that allow you to control how a property is accessed or modified. A getter is used to retrieve the value of a property, while a setter is used to assign a value to it. This gives you the ability to add logic when getting or setting a property, such as validation or transformation.
For example, consider a simple class with a getter and setter:
In this example, the value property is controlled by a getter and setter. The getter returns the current value of #count, while the setter ensures that the new value is always a number. This pattern is particularly useful in Svelte when combined with reactive state.
Building a Temperature Converter
Now, let’s apply this knowledge to build a Temperature Converter in Svelte. The converter will allow users to input a temperature in Celsius or Fahrenheit, and it will automatically update the other value based on the input. We’ll use a reactive class to manage the state and logic for this conversion.
Here’s the complete code for the TemperatureConverter class:
Let’s break this down step by step:
- Private Reactive State: The
#celsiusproperty is defined as a private reactive state using$state(0). This ensures that changes to#celsiustrigger reactivity in Svelte. - Getters and Setters: The
celsiusandfahrenheitproperties are controlled by getters and setters. The getters return the current value, while the setters handle the conversion logic. - Binding to UI: The
bind:valuedirective connects the input fields to thecelsiusandfahrenheitproperties, ensuring that the UI updates reactively. - Displaying Values: The current values of
celsiusandfahrenheitare displayed in the UI using curly braces{}.
When you run this code, you’ll see two input fields for Celsius and Fahrenheit. As you type in one field, the other field will update automatically based on the conversion logic. For example, if you enter 100 in the Celsius field, the Fahrenheit field will display 212.
