Skip to content

Commit

Permalink
feat: make useWishlistData initial data lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed Jul 28, 2023
1 parent e22aea5 commit 8f6ffc8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/src/AssetList/AssetListExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const AssetListExample: React.FC<{}> = () => {

<Wishlist.Component
// @ts-expect-error: TODO: update the new useWishlistData api.
initialData={list}
initialData={() => list}
style={styles.listContainer}
initialIndex={0}
>
Expand Down
2 changes: 1 addition & 1 deletion example/src/Chat/ChatExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const START_LOADING_ITEM = { type: 'loading', key: 'start-loading' } as any;
const END_LOADING_ITEM = { type: 'loading', key: 'end-loading' } as any;

export default function App() {
const data = useWishlistData<ChatItem>([]);
const data = useWishlistData<ChatItem>(() => []);
const [loading, setLoading] = useState(true);
const loadingStartRef = useRef(false);
const loadingEndRef = useRef(false);
Expand Down
4 changes: 2 additions & 2 deletions src/WishlistData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface WishlistDataInternal<T extends Item> extends WishlistData<T> {
* Creates an instance of Wishlist data which can be passed to Wishlist components.
*/
export function useWishlistData<T extends Item>(
initialData: Array<T>,
getInitialData: () => Array<T>,
): WishlistData<T> {
const dataId = useGeneratedId();
const getWishlistData = useMemo((): (() => WishlistDataInternal<T>) => {
Expand All @@ -41,7 +41,7 @@ export function useWishlistData<T extends Item>(
return global.dataCtx[dataId] as WishlistDataInternal<T>;
}

const currentlyRenderedCopy = createItemsDataStructure(initialData);
const currentlyRenderedCopy = createItemsDataStructure(getInitialData());
const attachedListIds = new Set<string>();
const pendingUpdates: Array<UpdateJob<T, unknown>> = [];

Expand Down

0 comments on commit 8f6ffc8

Please sign in to comment.