Action return data lets you send information back after a form or event happens. It helps update the page or show messages without reloading.
Action return data in Svelte
export const actions = { default: async ({ request }) => { const formData = await request.formData(); // process data return { success: true, message: 'Saved!' }; } };
The actions object holds functions that run on form submission.
Each action returns an object with data to send back to the page.
export const actions = { default: async ({ request }) => { const formData = await request.formData(); const name = formData.get('name'); if (!name) { return { error: 'Name is required' }; } return { success: true, name }; } };
export const actions = { save: async ({ request }) => { const data = await request.formData(); // save data logic return { saved: true }; }, delete: async ({ request }) => { // delete logic return { deleted: true }; } };
This SvelteKit example uses +page.svelte and +page.server.js. When submitted, it checks if the name is given. If not, it shows an error. If yes, it greets the user by name.
<!-- +page.svelte --> <script> export let data; </script> <form method="post"> <input name="name" placeholder="Your name" /> <button type="submit">Send</button> </form> {#if data?.error} <p style="color: red;">{data.error}</p> {:else if data?.success} <p style="color: green;">Hello, {data.name}!</p> {/if} <!-- +page.server.js --> export const actions = { default: async ({ request }) => { const formData = await request.formData(); const name = formData.get('name'); if (!name) { return { error: 'Name is required' }; } return { success: true, name }; } };
Returned data from actions is available in the page as data.
Use optional chaining like data?.error to avoid errors if no data yet.
Actions run on the server but return data to update the client smoothly.
Action return data sends info back after form events.
Use it to show success, errors, or update UI without reload.
Returned data is accessed in the page as data.
Practice
Solution
Step 1: Understand what actions do in SvelteKit
Actions handle form submissions and can return data to the page.Step 2: Identify the purpose of returned data
Returned data is used to update the UI or show messages without reloading the page.Final Answer:
To send information back to the page after a form event -> Option AQuick Check:
Action return data = send info back [OK]
- Thinking actions reload the page
- Confusing return data with navigation
- Assuming return data prevents form submission
Solution
Step 1: Review SvelteKit action return syntax
Actions return plain objects with data to send back to the page.Step 2: Check each option
return { success: true, message: 'Saved!' }; returns an object with success and message keys, which is correct. Options A, C, and D use incorrect or unrelated syntax.Final Answer:
return { success: true, message: 'Saved!' }; -> Option BQuick Check:
Return object with data = return { success: true, message: 'Saved!' }; [OK]
- Using response.send() which is not SvelteKit syntax
- Trying to redirect instead of returning data
- Calling window methods inside action
export const actions = {
default: async ({ request }) => {
const formData = await request.formData();
const name = formData.get('name');
if (!name) {
return { error: 'Name is required' };
}
return { success: `Hello, ${name}!` };
}
};What will
data contain if the form is submitted with name = "Anna"?Solution
Step 1: Analyze the form data handling
The action reads the 'name' field from the form data and checks if it exists.Step 2: Determine returned data for name = 'Anna'
Since 'Anna' is provided, the condition for missing name is false, so it returns { success: `Hello, Anna!` }.Final Answer:
{ success: 'Hello, Anna!' } -> Option DQuick Check:
Valid name returns success message [OK]
- Assuming error is returned even when name exists
- Expecting data to contain raw form fields
- Confusing undefined with returned object
export const actions = {
default: async ({ request }) => {
const formData = await request.formData();
const age = formData.get('age');
if (age < 18) {
return { error: 'Must be 18 or older' };
}
return { success: true };
}
};Solution
Step 1: Understand formData.get() returns strings
The age variable is a string, so comparing it with number 18 directly is incorrect.Step 2: Identify the error in comparison
Comparing string with number causes unexpected behavior; age should be converted to number first.Final Answer:
age is a string, so numeric comparison fails -> Option CQuick Check:
formData.get() returns string, convert before compare [OK]
- Forgetting formData.get() returns string
- Assuming implicit conversion works correctly
- Missing await on async calls
<form method="POST">
<input name="email" type="email" />
<button type="submit">Subscribe</button>
</form>
<script>
export let data;
</script>
<p>{data?.success ? data.success : ''}</p>Solution
Step 1: Understand how action return data updates UI
Returning an object with a success message allows the page to access it via the data prop.Step 2: Check the page code usage
The page uses {data?.success} to conditionally show the message without reloading.Final Answer:
Return { success: 'Subscribed!' } from the action and use {data.success} in the page -> Option AQuick Check:
Return data + conditional render updates UI [OK]
- Trying to alert inside action (server-side)
- Reloading or redirecting instead of returning data
- Not using returned data in the page
Ready to prove it?
Pass the test and this pattern moves unknown
