MCPcopy
hub / github.com/nytimes/react-tracking / WithTracking

Function WithTracking

src/withTrackingComponentDecorator.js:16–57  ·  view source on GitHub ↗
({ rtFwdRef, ...props })

Source from the content-addressed store, hash-verified

14 DecoratedComponent.displayName || DecoratedComponent.name || 'Component';
15
16 function WithTracking({ rtFwdRef, ...props }) {
17 const latestProps = useRef(props);
18
19 useEffect(() => {
20 // keep the latest props in a mutable ref object to avoid creating
21 // additional dependency that could cause unnecessary re-renders
22 // see https://reactjs.org/docs/hooks-faq.html#what-can-i-do-if-my-effect-dependencies-change-too-often
23 latestProps.current = props;
24 });
25
26 const trackingDataFn = useCallback(
27 () =>
28 typeof trackingData === 'function'
29 ? trackingData(latestProps.current)
30 : trackingData,
31 []
32 );
33
34 const contextValue = useTrackingImpl(trackingDataFn, options);
35
36 const trackingProp = useMemo(
37 () => ({
38 trackEvent: contextValue.tracking.dispatch,
39 getTrackingData: contextValue.tracking.getTrackingData,
40 }),
41 [contextValue]
42 );
43
44 const propsToBePassed = useMemo(
45 () => (forwardRef ? { ...props, ref: rtFwdRef } : props),
46 [props, rtFwdRef]
47 );
48
49 return (
50 <ReactTrackingContext.Provider value={contextValue}>
51 {React.createElement(DecoratedComponent, {
52 ...propsToBePassed,
53 tracking: trackingProp,
54 })}
55 </ReactTrackingContext.Provider>
56 );
57 }
58
59 if (forwardRef) {
60 const forwarded = React.forwardRef((props, ref) =>

Callers

nothing calls this directly

Calls 1

useTrackingImplFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…