From 624f2669ef7d09f8dc98e19f8d85932641b68309 Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Sat, 25 May 2024 15:58:07 +0200 Subject: [PATCH] Suspense withTracker Based on @mfeuermann #393 --- .../react-meteor-data/suspense/withTracker.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 packages/react-meteor-data/suspense/withTracker.ts 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; + }; +};