[react-tracking] Add new Context api (#35504)

This commit is contained in:
Christopher Pappas 2019-05-15 23:12:15 -10:00 committed by Eloy Durán
parent 695b694eb9
commit e5ac06081f
2 changed files with 23 additions and 2 deletions

View File

@ -1,4 +1,4 @@
// Type definitions for react-tracking 5.0
// Type definitions for react-tracking 6.0
// Project: https://github.com/NYTimes/react-tracking
// Definitions by: Eloy Durán <https://github.com/alloy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@ -58,6 +58,12 @@ type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFuncti
type MethodDecorator = <T>(target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>;
export type Decorator = ClassDecorator & MethodDecorator;
export type TrackingContext<T = any> = React.Context<{
tracking: Options<T> & { data?: {} }
}>;
export const ReactTrackingContext: TrackingContext;
/**
* This is the type of the `track` function. Its declared as an interface so that consumers can extend the typing and
* specify defaults, such as a global analytics schema for the tracking-info.

View File

@ -1,5 +1,5 @@
import * as React from 'react';
import { Track, track as _track, TrackingProp, Options, Decorator } from 'react-tracking';
import { Track, track as _track, TrackingProp, Options, Decorator, TrackingContext, ReactTrackingContext } from 'react-tracking';
function customEventReporter(data: { page?: string }) {}
@ -70,3 +70,18 @@ class Test extends React.Component<any, null> {
);
}
}
const TestContext = () => {
const trackingContext = {
tracking: {
data: { foo: 'bar' },
dispatch: (data: {}) => data,
process: (x: string) => x
}
};
return (
<ReactTrackingContext.Provider value={trackingContext}>
<div>hello how are you</div>
</ReactTrackingContext.Provider>
);
};