react-tracking icon indicating copy to clipboard operation
react-tracking copied to clipboard

Attempting to call `useTracking` without a ReactTrackingContext present

Open basickarl opened this issue 4 years ago • 3 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'?

basickarl avatar Aug 06 '21 06:08 basickarl

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.

tizmagik avatar Aug 19 '21 20:08 tizmagik

Thanks, that fixed the issue!

basickarl avatar Aug 20 '21 08:08 basickarl

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

tizmagik avatar Aug 21 '21 20:08 tizmagik