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.

`useSlate`/`useSlateStatic` hook must be used inside the <Slate> component's context?

default discord avatar
see.esslast year

Trying to set up a custom

slate

component on v3 beta 125. I'm running into issues that my custom component isn't running within the Slate context.



import type { RichTextCustomElement } from "@payloadcms/richtext-slate";

export const SlateButton: RichTextCustomElement = {
  name: 'button',
  Button: "@/payload/blocks/content-area/elements/button/toolbar-button#ToolbarButtonButton",
  Element: "@/payload/blocks/content-area/elements/button/element#ButtonElement",
  plugins: ["@/payload/blocks/content-area/elements/button/plugin#WithInlineButton"],
};


export const ButtonElement: React.FC<any> = (props) => {
  const { attributes, children, editorRef, element, fieldProps, schemaPath } =
    useElement<LinkButtonProps>()  // this works fine, new in beta.119
  const { style = "primary", button_text, href } = element;
  const [showUrl, setShowUrl] = React.useState(false);


export const ToolbarButtonButton: React.FC<{ path: string }> = ({ path }) => {
  const { openModal, toggleModal } = useModal();
  const editor = useSlateStatic() // <--- FAILS HERE


'use client';

import { useSlatePlugin } from "@payloadcms/richtext-slate/client";
import type { Editor } from 'slate'

type Plugin = (editor: Editor) => Editor;

const inlineButton: Plugin = (incomingEditor: Editor): Editor => {
  const editor = incomingEditor;
  const { isVoid } = editor;

  editor.isVoid = (element) =>
    (element as any).type === "button" ? true : isVoid(element);

  return editor;
};

export const WithInlineButton = () => {
  useSlatePlugin('withInlineButton', inlineButton)
  return null
}


Any ideas?



The "Slate.js plugin | Error: The useSlate hook must be used inside the <Slate> component's context." link in the community help solved my issue -- removed those packages from my package.json and everything works.

    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.