Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Error: The useSlate hook must be used inside the <SlateProvider> component's context #167

Open
avoinea opened this issue Oct 15, 2021 · 17 comments
Assignees
Labels
bug Something isn't working

Comments

@avoinea
Copy link
Member

avoinea commented Oct 15, 2021

Latest volto-slate release fails with:

Error: The useSlate hook must be used inside the <SlateProvider> component's context 
@avoinea avoinea added the bug Something isn't working label Oct 15, 2021
@silviubogan
Copy link
Contributor

@avoinea The latest release is on the branch master? I see that the master branch has this tip: 41ae74e and it passes all the CI checks. Am I wrong?

@avoinea
Copy link
Member Author

avoinea commented Oct 15, 2021

@silviubogan I skipped the cypress tests in order to release it 😄 My fault here.

Still I think this error is not catched by the cypress tests.

@silviubogan
Copy link
Contributor

It seems that the toolbar button in volto-slate-metadata-mentions does not receive the Slate context so it crashes the IMS Frontend.

The first access of the Slate context with the useSlate hook does not find the context. It is first called when focusing a Volto-Slate based block. When making a new page the same bug appears when clicking the Volto-Slate Text block or going with the Down arrow key to the Volto-Slate Text block.

This bug appears in a new Volto-based frontend too, when using the latest version of KitKat in the develop branch. When using Volto-Slate develop, as in the latest commit in the previous link in this message, the bug is gone. I don't know yet if I can reproduce this working state in the IMS Frontend.

@silviubogan
Copy link
Contributor

The bug appears in the IMS Frontend with a new backend (no content other than default) too, although I've put the volto-slate branch develop and volto-eea-kitkat branch develop in mrs.developer.json, ran yarn develop and then yarn start. If I put, e.g. an window.alert, in the file src/addons/volto-slate/src/editor/index.js, it shows up in the browser well, so I'm missing something.

@silviubogan
Copy link
Contributor

  1. A small project that still throws the error: https://gist.github.com/silviubogan/acfb3d0dd090f5f3ab394da2fbc90806
  2. A small project that does not throw the error anymore: https://gist.github.com/silviubogan/86bc515982a4a9c55753af5bc0b7fecd

Any ideas?

@tiberiuichim
Copy link
Member

try to define the components as functions, not anonymous functions.

@silviubogan
Copy link
Contributor

@tiberiuichim I've made it like this:

const Btn = function () {
  useSlate();
  return null;
};

then like

function Btn() {
  useSlate();
  return null;
}

and they still throw the same error.

@tiberiuichim
Copy link
Member

This happens when the following conditions are met:

  • volto-slate is a workspace package (so it's in development mode)
  • volto addons have direct dependency on volto-slate (correct should be a peer dependency)
  • we have (not sure about this one) a hard resolution on a volto-slate dependency.

This cause each volto addon with a volto-slate dependency to keep its own copy of volto-slate, in its local node_modules, so it doesn't get hoisted.

Tale-tell: a warning when running yarn:

warning Resolution field "volto-slate@4.2.1" is incompatible with requested version "volto-slate@5.1.2"

Action: remove all hard dependencies on volto-slate, replace them with a peerDependency (in each Volto addon that dependns on volto-slate).

@tiberiuichim
Copy link
Member

I think my analysis is somewhat incorrect. I think it's safe for an addon to have another addon as a dependency, maybe we need to avoid the resolutions part?

@silviubogan
Copy link
Contributor

@tiberiuichim Does Volto work with nested addons?

@silviubogan
Copy link
Contributor

silviubogan commented Dec 5, 2021

@tiberiuichim Is eea/volto-block-image-cards#12 merged by mistake?

@silviubogan
Copy link
Contributor

@tiberiuichim

I think my analysis is somewhat incorrect. I think it's safe for an addon to have another addon as a dependency, maybe we need to avoid the resolutions part?

If one addon is installed twice, each time a different version, does it work well in Volto?

@tiberiuichim
Copy link
Member

tiberiuichim commented Dec 6, 2021

In principal having two different versions of a JS package bundled is not a problem. In our practice we want to avoid this behavior, as it leads to ugly situations like the one in this ticket.

How we avoid that situation is not clear to me, though. It seems that simple resolutions is not enough to avoid the situation. Maybe instead of * we need to set the dependency version in our volto addons as ^5 or something like that?

@silviubogan
Copy link
Contributor

@tiberiuichim Please review this reversion PR: eea/volto-block-image-cards#15.

@nileshgulia1
Copy link
Member

This error still persists in latest release.

@tiberiuichim
Copy link
Member

@nileshgulia1 this happens when there's multiple versions of volto-slate in a deployment. Check that there's only one version.

@nileshgulia1
Copy link
Member

@tiberiuichim Thanks, I started to update few set of addons to use latest volto-slate.
eea/volto-slate-dataentity#3
eea/volto-embed#17
eea/volto-datablocks#35

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants