docs: root hooks (#7160)
This commit is contained in:
@@ -18,8 +18,9 @@ There are many use cases for Hooks, including:
|
||||
- Send emails when contact forms are submitted
|
||||
- Track data ownership or changes over time
|
||||
|
||||
There are three main types of Hooks in Payload:
|
||||
There are four main types of Hooks in Payload:
|
||||
|
||||
- [Root Hooks](#root-hooks)
|
||||
- [Collection Hooks](/docs/hooks/collections)
|
||||
- [Global Hooks](/docs/hooks/globals)
|
||||
- [Field Hooks](/docs/hooks/fields)
|
||||
@@ -29,6 +30,55 @@ There are three main types of Hooks in Payload:
|
||||
Payload also ships a set of _React_ hooks that you can use in your frontend application. Although they share a common name, these are very different things and should not be confused. [More details](../admin.hooks).
|
||||
</Banner>
|
||||
|
||||
## Root Hooks
|
||||
|
||||
Root Hooks are not associated with any specific Collection, Global, or Field. They are useful for globally-oriented side effects, such as when an error occurs at the application level.
|
||||
|
||||
To add Root Hooks, use the `hooks` property in your [Payload Config](/docs/configuration/config):
|
||||
|
||||
```ts
|
||||
import { buildConfig } from 'payload'
|
||||
|
||||
export default buildConfig({
|
||||
// ...
|
||||
// highlight-start
|
||||
hooks: {
|
||||
afterError: () => {...}
|
||||
},
|
||||
// highlight-end
|
||||
})
|
||||
```
|
||||
|
||||
The following options are available:
|
||||
|
||||
| Option | Description |
|
||||
|--------------|-----------------------------------------------------------------------------------------------|
|
||||
| **`afterError`** | Runs after an error occurs in the Payload application. |
|
||||
|
||||
### afterError
|
||||
|
||||
The `afterError` Hook is triggered when an error occurs in the Payload application. This can be useful for logging errors to a third-party service, sending an email to the development team, logging the error to Sentry or DataDog, etc.
|
||||
|
||||
```ts
|
||||
import { buildConfig } from 'payload'
|
||||
|
||||
export default buildConfig({
|
||||
// ...
|
||||
hooks: {
|
||||
afterError: async ({ error }) => {
|
||||
// Do something
|
||||
}
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
The following arguments are provided to the `afterError` Hook:
|
||||
|
||||
| Argument | Description |
|
||||
|----------|-----------------------------------------------------------------------------------------------|
|
||||
| **`error`** | The error that occurred. |
|
||||
| **`context`** | Custom context passed between Hooks. [More details](./context). |
|
||||
|
||||
## Async vs. Synchronous
|
||||
|
||||
All Hooks can be written as either synchronous or asynchronous functions. Choosing the right type depends on your use case, but switching between the two is as simple as adding or removing the `async` keyword.
|
||||
|
||||
Reference in New Issue
Block a user