Given that a
CustomBlockgets wrapped in
BlockComponentContext.Provider(see [here](
https://github.com/payloadcms/payload/blob/main/packages/richtext-lexical/src/features/blocks/client/component/BlockContent.tsx#L111), I assume that we should be able to use
useBlockComponentContextin our custom blocks right? Wanted to check before I do a PR to add this export.
Thanks!
I have exported it here:
https://github.com/payloadcms/payload/pull/9896However you don't need to use these hooks. You can import the components directly - they will run the
useBlockComponentContexthook for you
See
https://github.com/payloadcms/payload/blob/main/test/fields/collections/Lexical/blockComponents/BlockComponent.tsxHmm I saw that, yet the hardcoded
keyin there - which I assume is a reference to a field's name - is something I'd like to avoid. I was hoping to find a way to get the fields defined on the Block. Like the ones defined here:
{
slug: 'myBlockWithBlock',
admin: {
components: {
Block: '/collections/Lexical/blockComponents/BlockComponent.js#BlockComponent',
},
},
fields: [
{
name: 'key',
label: () => {
return 'Key'
},
type: 'select',
options: ['value1', 'value2', 'value3'],
},
],
},Sure you can do that using the
useAllFormFields()hook. If the field name you need is known beforehand it's better to use the
useFormFields()hook though, that way you avoid unnecessary rerenders
Ok, that's good to know! Will play round with it some more, thanks so far!
Star
Discord
online
Get dedicated engineering support directly from the Payload team.