Using Action Parameters in Svelte
📖 Scenario: You are building a simple interactive box on a webpage. You want to change the box's background color when the user clicks on it. To do this, you will use a Svelte action that accepts a color parameter.
🎯 Goal: Create a Svelte component with a colored box. Use an action that changes the box's background color based on a parameter passed to the action.
📋 What You'll Learn
Create a Svelte action function named
colorChange that accepts a parameter for the color.Create a
color variable with the value 'lightblue'.Use the
colorChange action on a <div> element, passing the color variable as a parameter.The box should have a fixed size and show the background color from the action parameter.
💡 Why This Matters
🌍 Real World
Actions in Svelte let you add reusable behavior to HTML elements, like changing styles or handling events, which is common in interactive web apps.
💼 Career
Understanding how to use actions with parameters helps you build clean, modular, and accessible UI components in Svelte, a popular modern frontend framework.
Progress0 / 4 steps