Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pure is always there as prop I think

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;
};
};