eea / volto-slate

DEPRECATED: Moved to Volto Core since 16.0.0.alpha.15
MIT License
31 stars 11 forks source link

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

Open avoinea opened 2 years ago

avoinea commented 2 years ago

Latest volto-slate release fails with:

Error: The useSlate hook must be used inside the <SlateProvider> component's context 
silviubogan commented 2 years ago

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

avoinea commented 2 years ago

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

silviubogan commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago
  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 commented 2 years ago

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

silviubogan commented 2 years ago

@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 commented 2 years ago

This happens when the following conditions are met:

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 commented 2 years ago

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 commented 2 years ago

@tiberiuichim Does Volto work with nested addons?

silviubogan commented 2 years ago

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

silviubogan commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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

nileshgulia1 commented 2 years ago

This error still persists in latest release.

tiberiuichim commented 2 years ago

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

nileshgulia1 commented 2 years ago

@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