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

Consider adding an option "onLoad"? #48

Open
freewind opened this issue Jan 11, 2021 · 3 comments
Open

Consider adding an option "onLoad"? #48

freewind opened this issue Jan 11, 2021 · 3 comments

Comments

@freewind
Copy link

I see there are some options like onSuccess, onError which allow us to do something that has side-effects, but no one likeonLoad when the fetching is started.

It might be useful if we want to reset something e.g. selected value of a dropdown, before we fetching new options.

I tried several solutions:

  1. do it in the fetching function:
const fetchOptions = useAsync(async () => {
  store.selectedValue = null;
  return await fetchOptionsFromRemote();
}, [])
  1. use another useEffect:
const fetchOptions = useAsync(async () => {
  return await fetchOptionsFromRemote();
}, [])

useEffect(() => {
  if(fetchOptions.loading) {
    store.selectedValue = null;
  }
}, [fetchOptions.loading]);

But neither looks as great as we can just:

const fetchOptions = useAsync(async () => {
  return await fetchOptionsFromRemote();
}, [], {
  onLoad: () => {  store.selectedValue = null }
})
@pig800509
Copy link

const { execute } = useAsyncCallback(fetchOptionsFromRemote);

const fetchOptions = useCallback(execute,[]);
useEffect(() => {
  fetchOptions().then(() => {
    store.selectedValue = null;
  });
}, [fetchOptions]);

or use setResult in option?

@freewind
Copy link
Author

@pig800509 If I understand correctly, your solution looks like doing something after loading (there is already an onSucess for this). I want to do something before loading started.

@pig800509
Copy link

@freewind

const fetchOptions = useAsync(async () => {
  store.selectedValue = null;  //do something here.
  return await fetchOptionsFromRemote();
}, [])

What is different with "onLoad" options?

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