Attempting to call `useTracking` without a ReactTrackingContext present
basickarl opened this issue · comments
Hello! Using a tracker within a functional component and this pops up. Any idea what this could refer to?
Attempting to call 'useTracking' without a ReactTrackingContext present. Did you forget to wrap the top of your component tree with 'track'?
Ah yep, sorry, the hook can't be used until there's at least a top-level wrapped component with track()
:
/* App.js */
import React from 'react';
import track from 'react-tracking';
const App = () => <div />;
const TrackedApp = track()(App); // Now anything inside of App can use the Hook
export default TrackedApp; // Export the TrackedApp instead of the raw App directly
We should make this more obvious in the docs. I'd also be open to a PR to update the behavior of the hook so that it establishes the top-level tracking context if it's not there already, if that's possible/desirable.
Thanks, that fixed the issue!
Glad to clarify! I’m actually going to reopen this issue so we can make it more obvious in the docs/update this behavior. Thanks again for opening the issue