volto-slate icon indicating copy to clipboard operation
volto-slate copied to clipboard

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

Open avoinea opened this issue 4 years ago • 17 comments

Latest volto-slate release fails with:

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

avoinea avatar Oct 15 '21 11:10 avoinea

@avoinea The latest release is on the branch master? I see that the master branch has this tip: https://github.com/eea/volto-slate/commit/41ae74efd58e9f8a2ca21ece6ee8181bc299fe6b and it passes all the CI checks. Am I wrong?

silviubogan avatar Oct 15 '21 12:10 silviubogan

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

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

avoinea avatar Oct 15 '21 12:10 avoinea

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 avatar Nov 26 '21 14:11 silviubogan

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 avatar Nov 26 '21 15:11 silviubogan

  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?

silviubogan avatar Nov 26 '21 16:11 silviubogan

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

tiberiuichim avatar Nov 26 '21 17:11 tiberiuichim

@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.

silviubogan avatar Nov 27 '21 16:11 silviubogan

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 "[email protected]" is incompatible with requested version "[email protected]"

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

tiberiuichim avatar Dec 03 '21 14:12 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?

tiberiuichim avatar Dec 03 '21 17:12 tiberiuichim

@tiberiuichim Does Volto work with nested addons?

silviubogan avatar Dec 05 '21 22:12 silviubogan

@tiberiuichim Is https://github.com/eea/volto-block-image-cards/pull/12 merged by mistake?

silviubogan avatar Dec 05 '21 22:12 silviubogan

@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?

silviubogan avatar Dec 06 '21 12:12 silviubogan

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?

tiberiuichim avatar Dec 06 '21 12:12 tiberiuichim

@tiberiuichim Please review this reversion PR: https://github.com/eea/volto-block-image-cards/pull/15.

silviubogan avatar Dec 09 '21 11:12 silviubogan

This error still persists in latest release.

nileshgulia1 avatar Jan 07 '22 08:01 nileshgulia1

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

tiberiuichim avatar Jan 07 '22 09:01 tiberiuichim

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

nileshgulia1 avatar Jan 07 '22 10:01 nileshgulia1