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

Allow setting new data based on old data #103

Open
elsangedy opened this issue Sep 5, 2019 · 5 comments
Open

Allow setting new data based on old data #103

elsangedy opened this issue Sep 5, 2019 · 5 comments

Comments

@elsangedy
Copy link

Now is not possible update the current data inside deferFn, I only can replace with new data.

It would be nice if deferFn get the current state or data as a parameter, this can be able implement features like infinite scroll

const deferFn  = (params, defaultParams, state, abortCtrl) => Promise.resolve([...state.data, ...newDataArray])

...

const { data, run } = useAsync({ deferFn })
@elsangedy
Copy link
Author

@ghengeveld
Copy link
Member

ghengeveld commented Sep 6, 2019

Excellent suggestion! We certainly should support this usecase (updating data based on the previous data and the new data). This doesn't just apply to deferFn, but also promiseFn. Let's consider adding a transform option that looks like this:

transform: (newData, oldData, props, args) => newData + oldData // do whatever

What we need to consider:

  • What about reload? Do we skip the transform or do we apply it regardless? Probably passing a reload flag to the transform would help.
  • What about setData? I'd expect this one to ignore the transform (since it's a setter), but we may want it to accept a transform function as well.

@ghengeveld ghengeveld changed the title Get current state as a parameter in deferFn Allow setting new data based on old data Sep 6, 2019
@ghengeveld
Copy link
Member

@all-contributors please add @elsangedy for ideas

@allcontributors
Copy link
Contributor

@ghengeveld

I've put up a pull request to add @elsangedy! 🎉

@ghengeveld
Copy link
Member

For those who want this feature right now, here's how you can accomplish it: https://codesandbox.io/s/react-async-pagination-og13b

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