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

is this the correct syntax for deferFn #238

Open
dagda1 opened this issue Jan 6, 2020 · 2 comments
Open

is this the correct syntax for deferFn #238

dagda1 opened this issue Jan 6, 2020 · 2 comments

Comments

@dagda1
Copy link

dagda1 commented Jan 6, 2020

Hi,

First of all, I think this is a great project. Many thanks for the effort.

Is this the correct way to use deferFn

  const deferFn = ([args]: [Required<Credentials>]) =>
    cognitoService.confirmResetPassword(args);

  const { isPending, run } = useAsync({
    deferFn,
  });

  const [verificationCode, setVerificationCode] = useState<{
    verificationCode: string;
  }>();

I think I would prefer useFetch with {defer: true} but I am not using fetch for these calls.

Is there a better way or shy can deferFn no be like promiseFn only it is not executed immediately.

@ghengeveld
Copy link
Member

That looks fine, although I'd rename args to credentials because it's more descriptive of what it represents.

deferFn and promiseFn are very similar, the difference being:

  • deferFn is executed when you call run, while promiseFn is executed on first render.
  • deferFn takes one extra argument (args), which comes before the other two arguments.

@dagda1
Copy link
Author

dagda1 commented Jan 6, 2020

@ghengeveld thanks, i get why args has to be an array.

one more thing if you don’t mind.

i think this indexer in the AsyncOptions type is making the type not strong typed. i can have:

useAsync({ promiseFon: fn }) 

and it the compiler won’t recognise the wrong property.

you could take a second type argument and augment the options


export type AsyncOptions<T, P = {}> = {
  promise?: Promise<T>
  promiseFn?: PromiseFn<T>
  deferFn?: DeferFn<T>
  watch?: any
  watchFn?: (props: AsyncProps<T>, prevProps: AsyncProps<T>) => any
  initialValue?: T
  onResolve?: (data: T) => void
  onReject?: (error: Error) => void
  reducer?: (
    state: ReducerAsyncState<T>,
    action: AsyncAction<T>,
    internalReducer: (state: ReducerAsyncState<T>, action: AsyncAction<T>) => ReducerAsyncState<T>
  ) => AsyncState<T>
  dispatcher?: (
    action: AsyncAction<T>,
    internalDispatch: (action: AsyncAction<T>) => void,
    props: AsyncProps<T>
  ) => void
  debugLabel?: string
} & P;

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