Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass this.state.data to then(), catch(), andThen() and andCatch() functions for more powerful side effects #227

Open
nuschk opened this issue Mar 5, 2019 · 2 comments

Comments

@nuschk
Copy link

nuschk commented Mar 5, 2019

Does it make any sense to have this.state.data available in those handlers? In my case, it's relevant for implementing an infinite scroller, where each fetch adds to the previous data and I need to somehow merge it.

Here's how that would look like:

const connections = {
  widgets: '/api/widget/?limit=20',
  loadMore: offset => ({
    loadingMore: {
      url: `/api/widget/?limit=20&offset=${offset}`,
      andThen: (res, meta, data) => ({
        widgets: { value: data.widgets.value.concat(res) },
      }),
    },
  }),
};

My current workaround is to pass a getData() function to loadMore() from the component, which works, but isn't nice.

On another note, I would like to have that data even in direct mapping functions (loadMore()above), so I can do things like optimistic updates.

And, 👍 for this very nice library, thank you for writing it!

@ryanbrainard
Copy link
Contributor

Instead of passing getData() to loadMore(), what if you pass the actual data and then you'd have access to it in the andThen? Something like this:

onst connections = {
  widgets: '/api/widget/?limit=20',
  loadMore: (offset, data) => ({
    loadingMore: {
      url: `/api/widget/?limit=20&offset=${offset}`,
      andThen: (res, meta) => ({
        widgets: { value: data.widgets.value.concat(res) },
      }),
    },
  }),
};

@nuschk
Copy link
Author

nuschk commented Mar 12, 2019

Yes, I tried that one. This leads to problems when calling loadMore() multiple times before it completes (which, e.g., when using react-virtualized, is normal, as the user is free to scoll on at full speed). You end up accessing old versions of data which have been updated since the call to loadMore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants