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

Conflict with react-query devtools #1425

Open
tyteen4a03 opened this issue Sep 18, 2024 · 7 comments
Open

Conflict with react-query devtools #1425

tyteen4a03 opened this issue Sep 18, 2024 · 7 comments

Comments

@tyteen4a03
Copy link

When I have both PostHog Toolbar and React Query DevTools in my application, toggling Feature Flags stop working.

@pauldambra
Copy link
Member

hey @tyteen4a03

Can you share more info about what happens? Do you see any errors in the network panel or the console?

Is that this dev tools? https://tanstack.com/query/latest/docs/framework/react/devtools

@tyteen4a03
Copy link
Author

hey @tyteen4a03

Can you share more info about what happens? Do you see any errors in the network panel or the console?

Is that this dev tools? https://tanstack.com/query/latest/docs/framework/react/devtools

No warnings or errors, clicking on the toggle just instantly closes the toolbar without actually toggling the FF.

Yes, that's the package.

@pauldambra
Copy link
Member

Wild... do other interactions in the toolbar work?

@tyteen4a03
Copy link
Author

Wild... do other interactions in the toolbar work?

I just checked again, none of the click interactions work.

@rmanganiello
Copy link

rmanganiello commented Nov 4, 2024

Hey folks!

I was having the same issue and I realized that this is because Tanstack Query Devtools is adding a mousedown event listener that generates this wrong behavior:

image

image

In order to prevent this, I created a React component that adds a new event listener for the mousedown event type, stopping propagation to other event listeners.

import { useEffect } from 'react';

export const PosthogToolbarGuardAgainstTanstackDevtools = () => {
  useEffect(() => {
    let observer: MutationObserver | null = null;

    // Function to check and attach the event listener if the element is present
    const checkAndAttachListener = () => {
      const posthogToolbar = document.getElementById('__POSTHOG_TOOLBAR__');

      if (posthogToolbar) {
        const stopEvent = (e: MouseEvent) => {
          e.stopImmediatePropagation();
        };
        posthogToolbar.addEventListener('mousedown', stopEvent);

        // Cleanup function to remove the event listener and disconnect the observer
        return () => {
          posthogToolbar.removeEventListener('mousedown', stopEvent);
          if (observer) observer.disconnect();
        };
      }
    };

    // Observer callback triggers when any changes in child elements of the body occur
    observer = new MutationObserver(checkAndAttachListener);
    observer.observe(document.body, { childList: true, subtree: true });

    // Return cleanup function to disconnect observer on unmount
    return () => {
      if (observer) observer.disconnect();
    };
  }, []);

  return null;
};

You can use this component inside the PostHogProvider component like this:

<PostHogProvider apiKey={import.meta.env.VITE_POSTHOG_API_KEY} options={posthogOptions}>
  <PosthogToolbarGuardAgainstTanstackDevtools />
  {children}
</PostHogProvider>

Let me know if this works for you!

@tyteen4a03
Copy link
Author

@rmanganiello thank you! Do you think this is an issue on TanStack's end?

@rmanganiello
Copy link

Not sure yet, I need to keep checking the Tanstack Devtools Library to understand where the issue is generated.

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

3 participants