atomicojs / atomico

Atomico a micro-library for creating webcomponents using only functions, hooks and virtual-dom.
https://atomicojs.dev
MIT License
1.16k stars 43 forks source link

Cannot read properties of undefined (reading 'update') #67

Closed efoken closed 2 years ago

efoken commented 2 years ago

Getting those wierd errors all the time, when trying to register the WebComponent:

create-hooks.js:41 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'update')
    at useUpdate (create-hooks.js:41)
    at useState (hooks.js:14)
    at useSlot (use-slot.js:8)
    at Button (Button.tsx:3)
    at Atom._render (custom-element.js:28)
    at Object.load (create-hooks.js:95)
    at custom-element.js:69

Getting this with every component, no matter how simple it is. I'm using Atomico inside React Storybook, but also tried setting up a simple Webpack 5 Devserver, getting the same errors all the time...

You ever seen this?

UpperCod commented 2 years ago

Hi, there are 2 probable situations that can generate this error:

  1. Atomico duplicate: since atomico defines a global constant to identify the group of rendered hooks.
  2. A hook outside the hook render box: a hook executed outside of scope will not be able to read the global constant to identify the group of hooks.

Is it possible for you to share the repository with me?

efoken commented 2 years ago

Ah yes, I really have 2 versions of Atomico installed, that was the error, thanks for the tip 👍

But you know why I have 2 versions installed? This is because @atomico/hooks has a hard dependency on atomico@latest. So as soon as I use a fixed Atomico version in my project, which is not the latest version (and I do), there will be 2 versions installed. The best solution is when you define Atomico as a peerDependency instead hard dependency in the @atomico/hooks package 🙏

EDIT: I cannot share the repo with you, because it's an internal project for a customer. I tried to setup a runnable example but then saw the error with the 2 versions.

UpperCod commented 2 years ago

Good idea in the next version of the libraries that share a link with Atomico I will define this as peerDependencie

It is no longer necessary to share the repo with me, now I understand where the error comes from, thank you for telling me this