Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slate-react is an uncontrolled component in v0.67+ #4765

Closed
antsago opened this issue Jan 4, 2022 · 3 comments · Fixed by #4768
Closed

Slate-react is an uncontrolled component in v0.67+ #4765

antsago opened this issue Jan 4, 2022 · 3 comments · Fixed by #4768

Comments

@antsago
Copy link
Contributor

antsago commented Jan 4, 2022

Description
If you initiate a value change from outside of slate by calling setValue (e.g. from within a setTimeout), the value does not change, making the slate component behave as if it was an uncontrolled component. I've reproed it with slate-react 0.67.0 and later, but found it working with slate-react 0.66.7 and prior.

Recording

Enregistrement.de.l.ecran.2022-01-04.a.10.31.27.PM.mov

Sandbox
https://codesandbox.io/s/slate-reproductions-forked-4vb05

Steps
To reproduce the behavior:

  1. Refresh the page (from the sandbox)
  2. Wait for two seconds
  3. See the text fail to change but the corresponding message appear in the console

Expectation
The value the editor displays should change to match what the value passed to it.

Environment

  • Slate Version: 0.67.0-0.72.3
  • Operating System: macOS
  • Browser: Chrome

Context
The idea is to change the editor text from external (non-slate) events; the use of setTimeout is just a simple way of imitating that, not the actual use-case. I've also managed to get around it by wrapping Slate and Editor (and the createEditor useMemo) within another component and forcing it to re-render by giving it a key and changing it every time.

@dylans
Copy link
Collaborator

dylans commented Jan 4, 2022

See the discussion in #4540 for why this was changed.

@antsago
Copy link
Contributor Author

antsago commented Jan 4, 2022

@dylans thanks for pointing it out! I'll close this issue now and sorry I couldn't find the previous issues (of which there are quite a lot!). From what I read, rather than setValue, we should use something like:

  const externalSetValue = useCallback(
    (newValue) => {
      editor.children = newValue
      setValue(newValue)
    },
    [editor]
  )

Should I send a pr updating the documentation with that snippet?

@dylans
Copy link
Collaborator

dylans commented Jan 5, 2022

No worries. A PR updating the docs would be amazing, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants