Adds a dedicated "Custom Components" section to the docs. As users become familiar with building custom components, not all areas that support customization are well documented. Not only this, but the current pattern does not allow for deep elaboration on these concepts without their pages growing to an unmanageable size. Custom components in general is a large enough topic to merit a standalone section with subpages. This change will make navigation much more intuitive, help keep page size down, and provide room to document every single available custom component with snippets to show exactly how they are typed, etc. This is a substantial change to the docs, here is the overview: - The "Admin > Customizing Components" doc is now located at "Custom Components > overview" - The "Admin > Views" doc is now located at "Custom Components > Custom Views" - There is a new "Custom Components > Edit View" doc - There is a new "Custom Components > List View" doc - The information about root components within the "Admin > Customizing Components" doc has been moved to a new "Custom Components > Root Components" doc - The information about custom providers within the "Admin > Customizing Components" doc has been moved to a new "Custom Components > Custom Providers" doc Similar to the goals of #10743, #10742, and #10741. Fixes #10872 and initial scaffolding for #10353. Dependent on #11126. This change will require the following redirects to be set up: - `/docs/admin/hooks` → `/docs/admin/react-hooks` - `/docs/admin/components` → `/docs/custom-components/overview` - `/docs/admin/views` → `/docs/custom-components/views`
50 lines
1.7 KiB
Plaintext
50 lines
1.7 KiB
Plaintext
---
|
|
title: Swap in your own React Context providers
|
|
label: Custom Providers
|
|
order: 30
|
|
desc:
|
|
keywords: admin, components, custom, documentation, Content Management System, cms, headless, javascript, node, react, nextjs
|
|
---
|
|
|
|
As you add more and more [Custom Components](./overview) to your [Admin Panel](../admin/overview), you may find it helpful to add additional [React Context](https://react.dev/learn/scaling-up-with-reducer-and-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](../configuration/overview):
|
|
|
|
```ts
|
|
import { buildConfig } from 'payload'
|
|
|
|
export default buildConfig({
|
|
// ...
|
|
admin: {
|
|
components: {
|
|
providers: ['/path/to/MyProvider'], // highlight-line
|
|
},
|
|
},
|
|
})
|
|
```
|
|
|
|
Then build your Custom Provider as follows:
|
|
|
|
```tsx
|
|
'use client'
|
|
import React, { createContext, useContext } from 'react'
|
|
|
|
const MyCustomContext = React.createContext(myCustomValue)
|
|
|
|
export function MyProvider({ children }: { children: React.ReactNode }) {
|
|
return (
|
|
<MyCustomContext.Provider value={myCustomValue}>
|
|
{children}
|
|
</MyCustomContext.Provider>
|
|
)
|
|
}
|
|
|
|
export const useMyCustomContext = () => useContext(MyCustomContext)
|
|
```
|
|
|
|
_For details on how to build Custom Components, see [Building Custom Components](./overview#building-custom-components)._
|
|
|
|
<Banner type="warning">
|
|
**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.
|
|
</Banner>
|