Skip to content

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

@dmigra0

Description

@dmigra0

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>
  );
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions