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

error when component uses context #1

Closed
WickyNilliams opened this issue Mar 20, 2024 · 5 comments
Closed

error when component uses context #1

WickyNilliams opened this issue Mar 20, 2024 · 5 comments

Comments

@WickyNilliams
Copy link

WickyNilliams commented Mar 20, 2024

i have some components using context, and when i try SSR in astro i get the following error logged in the CLI terminal:

ReferenceError: CustomEvent is not defined
    at dispatchEvent (file:///path/to/project/node_modules/atomico/src/element/set-prototype.js:112:20)
    at ref.<computed> (file:///path/to/project/node_modules/atomico/src/hooks/custom-hooks/use-event.js:11:13)
    at detectValueFromProvider (file:///path/to/project/node_modules/atomico/src/context.js:52:9)
    at file:///path/to/project/node_modules/atomico/src/hooks/hooks.js:19:23
    at setState (file:///path/to/project/node_modules/atomico/src/hooks/state.js:15:17)
    at new State (file:///path/to/project/node_modules/atomico/src/hooks/state.js:21:9)
    at file:///path/to/project/node_modules/atomico/src/hooks/hooks.js:17:21
    at useHook (file:///path/to/project/node_modules/atomico/src/hooks/create-hooks.js:41:18)
    at useState (file:///path/to/project/node_modules/atomico/src/hooks/hooks.js:15:12)
    at useConsumer (file:///path/to/project/node_modules/atomico/src/context.js:62:55)

which surfaces in the browser as an astro error:

{"message":"The value defined for prop 'offset' must be of type 'Number'","target":{"dataset":{},"_props":{"offset":0},"mounted":{},"unmounted":{},"updated":{}},"value":"0"}

where offset is a prop in my component that consumes the context.

my astro config looks like this:

import { defineConfig } from "astro/config";
import Atomico from "@atomico/astro";

// https://astro.build/config
export default defineConfig({
  integrations: [Atomico()],
});

and my astro page looks like:

---
import "./path/to/components.js"
---

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>

  <body>
    <my-provider>
      <my-consumer></my-consumer>
    </my-provider>
  </body>
</html>

<script>
  import "./path/to/components.js"
</script>
@UpperCod
Copy link
Member

Hi @WickyNilliams , thank you for notifying this issue. At the moment, I am unable to replicate it. My reference code indicates that the context dispatches the event from a useState. This could fail if the host is equal to undefined, but even with SSG, it does not generate errors locally.

import { c, createContext, useContext } from "atomico";

export const MyContext = createContext({ value: 0 });

export const MyComponent = c(() => {
  const { value } = useContext(MyContext);
  return (
    <host shadowDom>
      <h1 style="color: white">contexto!</h1>
    </host>
  );
});

customElements.define("my-context", MyContext);
customElements.define("my-component", MyComponent);

Instance example 1:

import "./atomico";
---

<my-context>
  <my-component></my-component>
</my-context>

Instance example 2:

import { MyContext, MyComponent } from "./atomico";
---

<MyContext>
  <MyComponent/>
</MyContext>

Do you think it's possible for you to replicate that bug in this template? https://stackblitz.com/edit/atomico-astro?file=package.json

@WickyNilliams
Copy link
Author

From the stack trace I would guess that the context event is being fired on the server, since CustomEvent is not defined.

I'll try to create a reduced test case and report back.

@WickyNilliams
Copy link
Author

WickyNilliams commented Mar 21, 2024

OK here's a repro https://stackblitz.com/edit/atomico-astro-jkqadd

Looking at the code here, it will try to dispatch in the useState initializer fn, which will run on the server/build-time i think?

https://github.com/atomicojs/atomico/blob/7f128ed46e3b8bc87ae35db9bf8572f96bf60aab/src/context.js#L62-L64

@UpperCod
Copy link
Member

Thank you for the code. I have fixed this in atomico@1.77.2.

@WickyNilliams
Copy link
Author

Great thanks! This seems to have fixed this specific bug. But I've hit some other weirdness. I'll see if I can whip up a reduced test case Monday. Thanks again

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

No branches or pull requests

2 participants