Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

BlockComponentContext + useBlockComponentContext

default discord avatar
sebastianmulderslast year
4

Given that a

CustomBlock

gets 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

useBlockComponentContext

in our custom blocks right? Wanted to check before I do a PR to add this export.



Thanks!

  • discord user avatar
    alessiogr
    last year

    I have exported it here:

    https://github.com/payloadcms/payload/pull/9896

    However you don't need to use these hooks. You can import the components directly - they will run the

    useBlockComponentContext

    hook for you



    See

    https://github.com/payloadcms/payload/blob/main/test/fields/collections/Lexical/blockComponents/BlockComponent.tsx
  • default discord avatar
    sebastianmulderslast year

    Hmm I saw that, yet the hardcoded

    key

    in 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'],
                },
              ],
            },
  • discord user avatar
    alessiogr
    last year

    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

  • default discord avatar
    sebastianmulderslast year

    Ok, that's good to know! Will play round with it some more, thanks so far!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.