DOM-related Error with React Query and MSW in Storybook #30157
Unanswered
chosim-dvlpr
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
I’m encountering the following error when using React Query with MSW (Mock Service Worker) in Storybook:
createRoot(...): Target container is not a DOM element.
The error occurs depending on the configuration of the project’s entry point (
main.tsx
) and whether a React Query mutation (useFeedMutation
) is active. When theuseFeedMutation
in theFeedItem
component is commented out, the error does not occur. Below, I’ve detailed the issue and shared relevant code snippets, along with some questions I hope the community can help with.Problem Context
1. Project
main.tsx
CodeThe project’s entry point,
main.tsx
, renders the React application to the DOM. The error behavior changes based on the code used:1st Version: No Error in Storybook
2nd Version: Error Occurs in Storybook
2. Storybook-Related Code
FeedItem Component
The FeedItem component triggers a mutation via
useFeedMutation
from React Query. Notably, whenuseFeedMutation
is commented out, the error does not occur.preview.ts
The
preview.ts
file initializes MSW and sets up React Query and React Router for Storybook.FeedItem.stories.tsx
My Observations
Use of
useFeedMutation
useFeedMutation
is active in theFeedItem
component. If the mutation is commented out, the error does not occur. I think this suggests a potential timing issue between React Query’s state updates and Storybook’s DOM initialization.Questions for the Community
Any insights or advice from the community would be greatly appreciated! 🙏
Additional information
package.json
:Create a reproduction
No response
Beta Was this translation helpful? Give feedback.
All reactions