React Hooks
Payload provides a variety of powerful React Hooks that can be used within your own Custom Components, such as Custom Fields. With them, you can interface with Payload itself to build just about any type of complex customization you can think of.
useField
The useField
hook is used internally within all field components. It manages sending and receiving a field's state from its parent form. When you build a Custom Field Component, you will be responsible for sending and receiving the field's value
to and from the form yourself.
To do so, import the useField
hook as follows:
The useField
hook accepts the following arguments:
Property | Description |
---|---|
| If you do not provide a |
| A validation function executed client-side before submitting the form to the server. Different than Field-level Validation which runs strictly on the server. |
| If |
| If |
The useField
hook returns the following object:
useFormFields
There are times when a custom field component needs to have access to data from other fields, and you have a few options to do so. The useFormFields
hook is a powerful and highly performant way to retrieve a form's field state, as well as to retrieve the dispatchFields
method, which can be helpful for setting other fields' form states from anywhere within a form.
Thanks to the awesome package use-context-selector
, you can retrieve a specific field's state easily. This is ideal because you can ensure you have an up-to-date field state, and your component will only re-render when that field's state changes.
You can pass a Redux-like selector into the hook, which will ensure that you retrieve only the field that you want. The selector takes an argument with type of [fields: Fields, dispatch: React.Dispatch<Action>]]
.
useAllFormFields
To retrieve more than one field, you can use the useAllFormFields
hook. Your component will re-render when any field changes, so use this hook only if you absolutely need to. Unlike the useFormFields
hook, this hook does not accept a "selector", and it always returns an array with type of [fields: Fields, dispatch: React.Dispatch<Action>]]
.
You can do lots of powerful stuff by retrieving the full form state, like using built-in helper functions to reduce field state to values only, or to retrieve sibling data by path.
Updating other fields' values
If you are building a Custom Component, then you should use setValue
which is returned from the useField
hook to programmatically set your field's value. But if you're looking to update another field's value, you can use dispatchFields
returned from useFormFields
.
You can send the following actions to the dispatchFields
function.
Action | Description |
---|---|
| Adds a row of data (useful in array / block field data) |
| Duplicates a row of data (useful in array / block field data) |
| Updates a field's conditional logic result (true / false) |
| Moves a row of data (useful in array / block field data) |
| Removes a field from form state |
| Removes a row of data from form state (useful in array / block field data) |
| Completely replaces form state |
| Update any property of a specific field's state |
To see types for each action supported within the dispatchFields
hook, check out the Form types here.
useForm
The useForm
hook can be used to interact with the form itself, and sends back many methods that can be used to reactively fetch form state without causing rerenders within your components each time a field is changed. This is useful if you have action-based callbacks that your components fire, and need to interact with form state based on a user action.
The useForm
hook returns an object with the following properties:
Action | Description | Example |
---|---|---|
| Deprecated. This property cannot be relied on as up-to-date. | |
| Method to trigger the form to submit | |
| Dispatch actions to the form field state | |
| Trigger a validation of the form state | |
| Create a | |
| Boolean denoting whether or not the form is disabled | |
| Gets all fields from state | |
| Gets a single field from state by path | |
| Returns the data stored in the form | |
| Returns form sibling data for the given field path | |
| Set the form's | |
| Set the form's | |
| Set the form's | |
| The ref from the form HTML element | |
| Method to reset the form to its initial state | |
| Method to add a row on an array or block field | |
| Method to remove a row from an array or block field | |
| Method to replace a row from an array or block field |
useCollapsible
The useCollapsible
hook allows you to control parent collapsibles:
Property | Description |
---|---|
| State of the collapsible. |
| If nested, determine if the nearest collapsible is visible. |
| Toggles the state of the nearest collapsible. |
| Determine when you are within another collapsible. |
Example:
useDocumentInfo
The useDocumentInfo
hook provides information about the current document being edited, including the following:
Property | Description |
---|---|
| The user currently editing the document. |
| Either the Collection or Global config of the document, depending on what is being edited. |
| Whether the document is currently locked by another user. |
| If the doc is a collection, its ID will be returned |
| Method to retrieve document-level user preferences. |
| Method to retrieve document-level user preferences. |
| Whether the document has a published version. |
| Method to increment the version count of the document. |
| The |
| Versions of the current doc. |
| Unpublished versions of the current doc. |
| The currently published version of the doc being edited. |
| Method to retrieve document versions. |
| The current documents permissions. Collection document permissions fallback when no id is present (i.e. on create). |
| The current version count of the document. |
Example:
useListQuery
The useListQuery
hook is used to subscribe to the data, current query, and other properties used within the List View. You can use this hook within any Custom Component rendered within the List View.
The useListQuery
hook returns an object with the following properties:
Property | Description |
---|---|
| The data that is being displayed in the List View. |
| The default limit of items to display in the List View. |
| The default sort order of items in the List View. |
| A method to handle page changes in the List View. |
| A method to handle per page changes in the List View. |
| A method to handle search changes in the List View. |
| A method to handle sort changes in the List View. |
| A method to handle where changes in the List View. |
| The current query that is being used to fetch the data in the List View. |
useLocale
In any Custom Component you can get the selected locale object with the useLocale
hook. useLocale
gives you the full locale object, consisting of a label
, rtl
(right-to-left) property, and then code
. Here is a simple example:
useAuth
Useful to retrieve info about the currently logged in user as well as methods for interacting with it. It sends back an object with the following properties:
Property | Description |
---|---|
| The currently logged in user |
| A method to log out the currently logged in user |
| A method to trigger the silent refreshing of a user's auth token |
| Set the token of the user, to be decoded and used to reset the user and token in memory |
| The logged in user's token (useful for creating preview links, etc.) |
| Load new permissions (useful when content that effects permissions has been changed) |
| The permissions of the current user |
useConfig
Used to retrieve the Payload Client Config.
useEditDepth
Sends back how many editing levels "deep" the current component is. Edit depth is relevant while adding new documents / editing documents in modal windows and other cases.
usePreferences
Returns methods to set and get user preferences. More info can be found here.
useTheme
Returns the currently selected theme (light
, dark
or auto
), a set function to update it and a boolean autoMode
, used to determine if the theme value should be set automatically based on the user's device preferences.
useTableColumns
Returns methods to manipulate table columns
useDocumentEvents
The useDocumentEvents
hook provides a way of subscribing to cross-document events, such as updates made to nested documents within a drawer. This hook will report document events that are outside the scope of the document currently being edited. This hook provides the following:
Property | Description |
---|---|
| An object containing the most recently updated document. It contains the |
| A method used to report updates to documents. It accepts the same arguments as the |
Example: