diff --git a/packages/react-meteor-data/suspense/withTracker.ts b/packages/react-meteor-data/suspense/withTracker.ts new file mode 100644 index 0000000..d1ad0e7 --- /dev/null +++ b/packages/react-meteor-data/suspense/withTracker.ts @@ -0,0 +1,28 @@ +import React, { forwardRef, memo } from 'react'; +import { useTracker } from './useTracker'; + +type ReactiveFn = (props: object) => any; +type ReactiveOptions = { + getMeteorData: ReactiveFn; + pure?: boolean; + skipUpdate?: (prev: any, next: any) => boolean; +} + +export const withTracker = (options: ReactiveFn | ReactiveOptions) => { + return (Component: React.ComponentType) => { + const getMeteorData = typeof options === 'function' ? + options : + options.getMeteorData; + + const WithTracker = forwardRef((props, ref) => { + const data = useTracker('withTracker', + () => getMeteorData(props) || {}); + return ( + + ); + }); + + const { pure = true } = options; + return pure ? memo(WithTracker) : WithTracker; + }; +};