Skip to content

Commit

Permalink
Suspense withTracker
Browse files Browse the repository at this point in the history
Based on @mfeuermann #393
  • Loading branch information
StorytellerCZ committed May 25, 2024
1 parent c8f17c3 commit 624f266
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/react-meteor-data/suspense/withTracker.ts
Original file line number Diff line number Diff line change
@@ -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 (
<Component ref={ref} {...props} {...data} />
);
});

const { pure = true } = options;
return pure ? memo(WithTracker) : WithTracker;
};
};

0 comments on commit 624f266

Please sign in to comment.