nytimes / react-tracking

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

Convert WithTracking to function component #142

Closed bgergen closed 4 years ago

bgergen commented 4 years ago

This PR converts the WithTracking class to a function component, using hooks to access the context and dispatch on mount. This allows for better compatibility with libraries that use the legacy context API and old versions of hoist-non-react-statics, which introduced support for static contextType in v3.1.0 (relevant PR). I believe this will fix issue https://github.com/nytimes/react-tracking/issues/137 as well.

This also puts us in a better position to hopefully introduce an API for useTracking that will allow for contextual data to be passed in without wrapping components in the track decorator (issue https://github.com/nytimes/react-tracking/issues/138).

All e2e tests are passing. Unit tests for withTrackingComponentDecorator are mostly broken. I'm not entirely sure the best way to update these tests, as many of them test the internals of the WithTracking class, which are no longer accessible in this implementation.

I would like to do some further digging into whether there are any negative implications to memoizing so many functions with the useCallback hook, which we need to do in order to preserve function identity and avoid undesired rerenders of decorated components.