Skip to content
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

docs: Add JS code snippets for forms #54577

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ export default async function submit() {
}
```

```js filename="app/actions.js" switcher
'use server'

import { revalidatePath } from 'next/cache'

export default async function submit() {
await submitForm()
revalidatePath('/')
}
```

Or invalidate a specific data fetch with a cache tag using [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag):

```ts filename="app/actions.ts" switcher
Expand All @@ -198,6 +209,17 @@ export default async function submit() {
}
```

```js filename="app/actions.js" switcher
'use server'

import { revalidateTag } from 'next/cache'

export default async function submit() {
await addPost()
revalidateTag('posts')
}
```

</AppOnly>

### Redirecting
Expand Down Expand Up @@ -244,6 +266,19 @@ export default async function submit() {
}
```

```js filename="app/actions.js" switcher
'use server'

import { redirect } from 'next/navigation'
import { revalidateTag } from 'next/cache'

export default async function submit() {
const id = await addPost()
revalidateTag('posts') // Update cached posts
redirect(`/post/${id}`) // Navigate to new route
}
```

</AppOnly>

### Form Validation
Expand Down Expand Up @@ -740,7 +775,18 @@ You can set cookies inside a Server Action using the [`cookies`](/docs/app/api-r
import { cookies } from 'next/headers'

export async function create() {
const cart = await createCart():
const cart = await createCart()
cookies().set('cartId', cart.id)
}
```

```js filename="app/actions.js" switcher
'use server'

import { cookies } from 'next/headers'

export async function create() {
const cart = await createCart()
cookies().set('cartId', cart.id)
}
```
Expand Down Expand Up @@ -789,6 +835,17 @@ export async function create() {
}
```

```js filename="app/actions.js" switcher
'use server'

import { cookies } from 'next/headers'

export async function create() {
const auth = cookies().get('authorization')?.value
// ...
}
```

</AppOnly>

### Deleting Cookies
Expand Down Expand Up @@ -833,6 +890,17 @@ export async function create() {
}
```

```js filename="app/actions.js" switcher
'use server'

import { cookies } from 'next/headers'

export async function create() {
cookies().delete('name')
// ...
}
```

See [additional examples](/docs/app/api-reference/functions/cookies#deleting-cookies) for deleting cookies from Server Actions.

</AppOnly>