The warning 'props' is declared but its value is never read indicates that the props constant is declared but not used in the component. To resolve this, you should destructure the props directly if you need to use them. If you don't need to use the props, you can simply remove the declaration.
Here's how you can refactor your component to address this issue:
1. Define Your Types
First, define the types for your form data. You can do this in a separate TypeScript file or directly within your component.
// src/lib/types.ts
export interface FormData {
// Define the shape of your form data here
name: string;
email: string;
message: string;
}
2. Update Your Component
Refactor your Contact.svelte component to use $props() and manage the loading state directly. If you need to use the form prop, destructure it directly.
<script lang="ts">
import type { FormData } from '$lib/types';
// Destructure the form prop directly if needed
const { form } = $props<{ form?: FormData }>();
let loading: boolean = false;
// Simulate an async operation, e.g., fetching data or submitting a form
const handleSubmit = async (event: Event) => {
event.preventDefault();
loading = true;
// Simulate an async operation
await new Promise(resolve => setTimeout(resolve, 2000));
loading = false;
// Handle form submission logic here
};
</script>
<form on:submit={handleSubmit}>
<!-- Your form fields here -->
<button type="submit" class="btn btn-primary">
{#if loading}
Loading
{:else}
Send Offer
{/if}
</button>
</form>
3. Use the Component in a Route
If you need to pass data to the Contact component from a route, you can do so by passing props from the route page.
<!-- src/routes/+page.svelte -->
<script lang="ts">
import Contact from '$lib/components/Contact.svelte';
import type { FormData } from '$lib/types';
// Example data to pass to the Contact component
const initialData: FormData = {
name: '',
email: '',
message: ''
};
</script>
<Contact form={initialData} />
Summary
By destructuring the form prop directly from $props(), you can avoid the warning and make your code cleaner. This approach ensures that you only declare variables that are actually used, aligning with best practices for TypeScript and Svelte.