-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Data Mutation Guide #994
base: main
Are you sure you want to change the base?
Conversation
Run & review this pull request in StackBlitz Codeflow. |
✅ Deploy Preview for solid-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
👷 Deploy Preview for solid-docs processing.
|
ad657f8
to
6bb8293
Compare
e51f764
to
9ad48e6
Compare
const addPost = action(async (title: string) => { | ||
await fetch("https://my-api.com/posts", { | ||
method: "POST", | ||
body: JSON.stringify({ title }), | ||
}); | ||
}, "addPost"); | ||
|
||
export default function Page() { | ||
const [title, setTitle] = createSignal(""); | ||
return ( | ||
<form action={addPost.with(title())} method="post"> | ||
<input value={title()} onChange={(e) => setTitle(e.target.value)} /> | ||
<button>Add Post</button> | ||
</form> | ||
); | ||
} | ||
``` | ||
|
||
Note that the action takes a `string` as its parameter rather than a `FormData`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const addPost = action(async (title: string) => { | |
await fetch("https://my-api.com/posts", { | |
method: "POST", | |
body: JSON.stringify({ title }), | |
}); | |
}, "addPost"); | |
export default function Page() { | |
const [title, setTitle] = createSignal(""); | |
return ( | |
<form action={addPost.with(title())} method="post"> | |
<input value={title()} onChange={(e) => setTitle(e.target.value)} /> | |
<button>Add Post</button> | |
</form> | |
); | |
} | |
``` | |
Note that the action takes a `string` as its parameter rather than a `FormData`. | |
const addPost = action(async (title: string, formData: FormData) => { | |
await fetch("https://my-api.com/posts", { | |
method: "POST", | |
body: JSON.stringify({ title }), | |
}); | |
}, "addPost"); | |
export default function Page() { | |
const [title, setTitle] = createSignal(""); | |
return ( | |
<form action={addPost.with(title())} method="post"> | |
<input value={title()} onChange={(e) => setTitle(e.target.value)} /> | |
<button>Add Post</button> | |
</form> | |
); | |
} |
Note that the action now takes a string
as its first parameter and the FormData
as its second.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried not to go into details of APIs in this guide. I believe that this content is better suited for the API Reference.
return ( | ||
<div> | ||
<input value={title()} onInput={(e) => setTitle(e.target.value)} /> | ||
<button onClick={() => addPostAction(title())}>Add Post</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend adding a callout at the bottom of this section that explains why form actions are preferred over useAction
in terms of accessibility and progressive enhancement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is better suited for a tutorial.
Like the Data Fetching guide, this guide is geared toward people who want to know "how to" do things, and not why and when.
Co-authored-by: Dev Agrawal <[email protected]>
Co-authored-by: Dev Agrawal <[email protected]>
Description(required)
This is a follow up to #991.
This PR adds a data mutation guide
Related issues & labels