docs: update cookie example with `defineEventHandler` (#5405)

This commit is contained in:
Ash Go 2022-06-10 21:58:10 +08:00 committed by GitHub
parent 63b0457a69
commit 1449e70541
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 6 deletions

View File

@ -143,18 +143,16 @@ You can use `useCookie` and `setCookie` from [`h3`](https://github.com/unjs/h3)
**Example:**
```js
import { useCookie, setCookie } from 'h3'
export default (req, res) => {
export default defineEventHandler(event => {
// Read counter cookie
let counter = useCookie(req, 'counter') || 0
let counter = useCookie(event, 'counter') || 0
// Increase counter cookie by 1
setCookie(res, 'counter', ++counter)
setCookie(event, 'counter', ++counter)
// Send JSON response
return { counter }
}
})
```
:LinkExample{link="/examples/composables/use-cookie"}