Zustand store doesn't work when putting to external package #2870
Unanswered
roman-rookout
asked this question in
Q&A
Replies: 1 comment 5 replies
-
It sounds like it's that case. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi everyone. We have a set of different webViews, they are all independent react apps that can have a shared codebase, but run in an isolated manner. We started to build a lerna monorepo repository and split the codebase into different reusable packages.
We have multiple reusable infra zustand stores that will be placed in the shared package and be imported from different applications. Here, we encounter issues. After moving the store into the package, the app stopped loading. I built a test repo to reproduce the issue from the clean repository.
I have a package with a store:
const useTestStore = create<Store>((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
}));
I'm using it in the main component then
function App() {
const { count, increment, decrement } = useTestStore();
...
<p>{count}</p>
<button onClick={() => increment()}>
Increment
</button>
What I see is a warning about InvalidHooks usage outside of react component which is weird and "TypeError: Cannot read properties of null (reading 'useRef')".
It points to the row - const { count, increment, decrement } = useTestStore();
So any usage of hook will fail also useTestStore((state) => state.count)
Only "non hook" method like
useTestStore.getState().count
will work.Would appreciate for any input. Maybe we should reconsider the way we bundle the library, we are using Vite.
The PoC repo is here:
https://github.com/roman-rookout/zustand-package-test
Beta Was this translation helpful? Give feedback.
All reactions