As you add more and more Custom Components to your Admin Panel, you may find it helpful to add additional React Context(s) to your app. Payload allows you to inject your own context providers where you can export your own custom hooks, etc.
To add a Custom Provider, use the admin.components.providers property in your Payload Config:
1import { buildConfig } from 'payload'
3export default buildConfig({
7 providers: ['/path/to/MyProvider'],
Then build your Custom Provider as follows:
2import React, { createContext, use } from 'react'
4const MyCustomContext = React.createContext(myCustomValue)
6export function MyProvider({ children }: { children: React.ReactNode }) {
7 return <MyCustomContext value={myCustomValue}>{children}</MyCustomContext>
10export const useMyCustomContext = () => use(MyCustomContext)
For details on how to build Custom Components, see Building Custom Components.
Reminder: React Context exists only within Client Components. This means they must include the use client directive at the top of their files and cannot contain server-only code. To use a Server Component here, simply wrap your Client Component with it.