question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incremental loading for infinite scrolling

See original GitHub issue

I am implementing infinite scrolling for a list of tickets. Currently, recoil doesnt seem to offer a way to get all data that a selector has fetched.

Example:

export const issues = selector({
  key: 'issueList',
  get: async ({ get }) => {
    const cursor = get(issuesCursor);
    const client = getApolloClient();
    const issues = await client.query<IssuesQuery, IssuesQueryVariables>({
      query: IssuesDocument,
      variables: {
        cursor,
      },
    });
    return { nextCursor: issues.data.issuesCursor.cursor, items: issues.data.issuesCursor.items };
  },
});
export const issueList = selector({
  key: 'nextIssuesCursor',
  get: ({ get }) => get(issues).items,
});

I would like to receive all issues from the issueList selector.

I can solve this issue with a hook, yet i would strongly prefer keeping all the data fetching in recoiljs.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mastormcommented, Dec 3, 2020

I finished implementing the way @drarmstr recommended and it worked like a charm. Thanks a lot.

2reactions
drarmstrcommented, Nov 28, 2020

@mastorm / @BenjaBobs Would a solution using useRecoilCallback() like the following work?

export const allItems = atom<Array<Item>>>({
  key: 'allItems',
  default: [],
});

const currentCursorInternal = atom<number>({
  key: 'currentCursorInternal',
  default: 0,
});

export fetchMoreItems = useRecoilCallback(({snapshot, set}) => async () => {
  set(currentCursorInternal, cursor => {
    const next = cursor + 1;
    promiseDone(ApiCall(next), newData => {
      // TODO use some queue mechanism to handle out-of-order responses.
      set(allItems, existing => [...existing, ...newData]);
    });
    return next;
  });
});

But, the most versatile or appropriate for this situation might be using the waitForNone() helper to handle the incremental loading pattern:

const itemQuery = selectorFamily({
  key: 'ItemQuery',
  get: cursor => () => ApiCall(cursor),
});

const currentCursorInternal = atom<number>({
  key: 'currentCursorInternal',
  default: 0,
});

export function useFetchMoreItems() {
  const setCurrentCursor = useSetRecoilState(currentCursorInternal);
  return () => setCurrentCursor(cursor => cursor + 1);
}

export const allItems = selector({
  key: 'AllItems',
  get: ({get}) => {
    const current = get(currentCursorInternal);
    const cursorRange = Array.from(Array(current).keys());
    return get(waitForNone(cursorRange.map(itemQuery)));
  },
});

function ShowItemsComponent() {
  const itemLoadables = useGetRecoilValue(allItems);
  const fetchMoreItems = useFetchMoreItems();
  return (
    <div>
      <button onClick={fetchMoreItems}>Fetch More Items</button>
      {itemLoadables.map(itemLoadable => ({
        hasValue: <Item item={itemLoadable.contents} key={itemLoadable.contents} />,
        hasError: <Error error={itemLoadable.contents} key={itemLoadable.contents} />,
        loading: <CoolShimmer key={itemLoadable.contents} />,
      }[itemLoadable.state])}
    </div>
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinite Scrolling with Incremental Data Loading in Xamarin ...
CollectionView has built-in ability to fetch additional data when the user scrolls through the content already loaded. As the data threshold ...
Read more >
Implementing Infinite Scroll And Image Lazy Loading In React
The effect is to increment the value of pager.page by 1. Once this happens, the useEffect hook that has it as a dependency...
Read more >
Infinite Scrolling with Incremental Loading in Windows Phone 8
In mobile applications there's often a need for scrolling lists with large or even (for all practical purposes) infinite amounts of data ...
Read more >
c# - How to scrolling up and down using Incremental Loading ...
But I need the view to starts showing from middle of the list, and if scrolling up and down, the items should be...
Read more >
Options - Infinite Scroll
Loading. path. Determines the URL for the next page. Required. Set to a selector string of a link to the next page to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found