Slate: Slate component handlers should not be required to be pure

Created on 15 Jul 2019  Â·  6Comments  Â·  Source: ianstormtaylor/slate

From a code, I write, it looks like Slate component handlers are required to be pure. Yep, my code is 💯 React Hooks. When I create a new Slate handler with properly updated closure context, Slate is still using the previous reference. Slate component is not resilient. https://overreacted.io/writing-resilient-components/

This could be solved easily only with refactoring to Hooks and eslint-plugin-react-hooks.

improvement ♥ help

Most helpful comment

I have come across the same issue in my hooks app as well - it's bad because it means that callbacks passed in to slate (like handleEditorKeyDown) must be memoised, which means that they cannot use any dependencies like state or props.

Would be amazing if this was fixed soon - I'm not too familiar with the Slate internals, but happy to help if I can!

All 6 comments

In other words, Slate should allow dynamic event handlers. Workaround:

// Workaround for https://github.com/ianstormtaylor/slate/issues/2927
const historyIndexRef = useRef<number | null>(state.history.index);
historyIndexRef.current = state.history.index;

const handleEditorKeyDown = useCallback<
  (event: KeyboardEvent, editor: Controller, next: () => any) => any
>((event, _editor, next) => {
  // Disable writing in history mode.
  // TODO: We should also prevent pasting.
  if (historyIndexRef.current != null) {
    const isKeyNavigation =
      event.key === 'Tab' ||
      event.key === 'ArrowRight' ||
      event.key === 'ArrowDown' ||
      event.key === 'ArrowLeft' ||
      event.key === 'ArrowUp';
    if (!isKeyNavigation) event.preventDefault();
    return;
  }
  return next();
}, []); // Must be empty. https://github.com/ianstormtaylor/slate/issues/2927

I have come across the same issue in my hooks app as well - it's bad because it means that callbacks passed in to slate (like handleEditorKeyDown) must be memoised, which means that they cannot use any dependencies like state or props.

Would be amazing if this was fixed soon - I'm not too familiar with the Slate internals, but happy to help if I can!

In my project, I've used the same technique of using React refs to hold mutable versions of any dependencies that my callback relies on. Less than ideal, but works as a hacky temporary solution.

Open to ideas for this. The reason that handlers are memoized is because the handlers actually are combined to create their own "plugin", which affects the schema. And re-computing the plugins stack on every render seemed like a high cost. I can't remember the exact details.

But I'd love to have this restriction removed if possible, if someone wants to look into it.

I have a problem that's unrelated and I am brand new at this, so bear with me. I am having lots of issues with third parties or hackers getting in my phone and installing apps to watch listen steal or copy things from my phone, and they are to control my phone at times what do i do??

I believe that this may be fixed by https://github.com/ianstormtaylor/slate/pull/3093, which has changed a lot of the logic in Slate and slate-react especially. I'm going to close this out, but as always, feel free to open a new issue if it persists for you. Thanks for understanding.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bunterWolf picture bunterWolf  Â·  3Comments

Slapbox picture Slapbox  Â·  3Comments

ianstormtaylor picture ianstormtaylor  Â·  3Comments

JSH3R0 picture JSH3R0  Â·  3Comments

ezakto picture ezakto  Â·  3Comments