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

"Cannot call use outside of a molecule function" when exporting molecule from a package #76

Open
dmigra0 opened this issue Oct 2, 2024 · 4 comments

Comments

@dmigra0
Copy link

dmigra0 commented Oct 2, 2024

When exporting molecule with custom scope from an npm package (i.e @my_packages/bunshi_test).
In an app that uses the npm package I get the error:
Cannot call use outside of a molecule function

the package code (@my_packages/bunshi_test):

import { molecule, createScope, use } from 'bunshi';
export const TestScope = createScope('none');

export const TestMolecule = molecule(
  () => {
    use(TestScope);
    return 'test';
  }
);

test app:

import { useMolecule, ScopeProvider } from 'bunshi/react';
import { TestScope, TestMolecule } from '@my_packages/bunshi_test';

const Test = () => {
  const value = useMolecule(TestMolecule);
  console.log('value:', value);
  return null
}

const App = () => {
  return (
    <ScopeProvider scope={TestScope} value="graph1" >
      <Test />
    </ScopeProvider>
  );
};
@loganvolkers
Copy link
Member

You'll need to provide more details. This could be a side effect of the build system producing a bundled output, so you have two versions of bunshi in your final application.

@dmigra0
Copy link
Author

dmigra0 commented Nov 14, 2024

Hi, thanks for your answer.
I am using nx and vite bundler with set up peerDependecies there's quite a lot of moving parts.

cmd:
npm ls bunshi

returns the following:
[email protected]
├┬ [email protected]
│└─ [email protected] deduped
├┬ [email protected]
│└── [email protected] deduped
└─ [email protected]

@yjb94
Copy link

yjb94 commented Jan 2, 2025

@dmigra0 try importing from bunshi/dist/vanillainstead of bunshi. I am using bunshi with RN and some resolutions seems odd

@sproott
Copy link

sproott commented Jan 7, 2025

I got the same error in a Next.js app. It seems to be happening during SSR. I made sure the component using the molecule is not a Server Component, I do have a "use client" directive on top.

Is this library just not usable in SSR?

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

No branches or pull requests

4 participants