nytimes / react-tracking

🎯 Declarative tracking for React apps.
https://open.nytimes.com/introducing-react-tracking-declarative-tracking-for-react-apps-2c76706bb79a
Other
1.87k stars 123 forks source link

Attempting to call `useTracking` without a ReactTrackingContext present #185

Closed basickarl closed 1 year ago

basickarl commented 2 years ago

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'?

tizmagik commented 2 years ago

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.

basickarl commented 2 years ago

Thanks, that fixed the issue!

tizmagik commented 2 years ago

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