useEvaLogin
Description#
Hook that exposes handy helpers to login to EVA.
Options
mutationKey: string- Optional
- A mutation key can be set to inherit defaults set with queryClient.setMutationDefaults or to identify the mutation in the devtools.
onMutate: (variables: TVariables) => Promise<TContext | void> | TContext | void- Optional
- This function will fire before the mutation function is fired and is passed the same variables the mutation function would receive
- Useful to perform optimistic updates to a resource in hopes that the mutation succeeds
- The value returned from this function will be passed to both the onError and onSettled functions in the event of a mutation failure and can be useful for rolling back optimistic updates.
onSuccess: (data: TData, variables: TVariables, context?: TContext) => Promise<void> | void- Optional
- This function will fire when the mutation is successful and will be passed the mutation's result.
- If a promise is returned, it will be awaited and resolved before proceeding
onError: (err: TError, variables: TVariables, context?: TContext) => Promise<void> | void- Optional
- This function will fire if the mutation encounters an error and will be passed the error.
- If a promise is returned, it will be awaited and resolved before proceeding
onSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => Promise<void> | void- Optional
- This function will fire when the mutation is either successfully fetched or encounters an error and be passed either the data or error
- If a promise is returned, it will be awaited and resolved before proceeding
retry: boolean | number | (failureCount: number, error: TError) => boolean- If false, failed mutations will not retry by default.
- If true, failed mutations will retry infinitely.
- If set to an number, e.g. 3, failed mutations will retry until the failed mutations count meets that number.
retryDelay: (retryAttempt: number) => number- This function receives a retryAttempt integer and returns the delay to apply before the next attempt in milliseconds.
- A function like attempt => Math.min(attempt > 1 ? 2 -- attempt - 1000 : 1000, 30 - 1000) applies exponential backoff.
- A function like attempt => attempt - 1000 applies linear backoff.
useErrorBoundary- Defaults to the global query config's useErrorBoundary value, which is false
- Set this to true if you want mutation errors to be thrown in the render phase and propagate to the nearest error boundary
Returns
mutate: (variables: EVA.Core.Login, { onSuccess, onSettled, onError }) => voidThe mutation function you can call with variables to trigger the mutation and optionally override options passed to useMutation.mutateAsync: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>Similar to mutate but returns a promise which can be awaited.status: string- Will be:
idleinitial status prior to the mutation function executing.loadingif the mutation is currently executing.errorif the last mutation attempt resulted in an error.successif the last mutation attempt was successful.isIdle,isLoading,isSuccess,isError: boolean variables derived fromstatus
- Will be:
data: EVA.Core.LoginResponse | undefined- Defaults to undefined
- The last successfully resolved data for the query.
error: null | TErrorThe error object for the query, if an error was encountered.reset: () => voidA function to clean the mutation internal state (i.e., it resets the mutation to its initial state).
Usage#
import { useEvaLogin } from '@springtree/eva-suite-react-hooks';
const Example = () => { const { data, error, isError, isIdle, isLoading, isPaused, isSuccess, mutate: doLogin, mutateAsync: doLoginAsync, reset, status, } = useEvaLogin();
return ( <> <button onClick={() => doLogin({ Username: 'TestUser', Password: 'Secure123', AsEmployee: true, OrganizationUnitID: 1, }) } > Login </button> <button onClick={() => doLoginAsync({ UserName: 'myfunnyusername', Password: 'SuperSecure123!', SelectFirstOrganizationUnit: true, AsEmployee: true, }) } > Login and return promise </button> </> );};
export default Example;