import * as React from 'react' ; import { createContext , useRef } from 'react' ; import { OnSuccess , OnFailure } from '../types' ; export const SideEffectContext = createContext < SideEffectContextValue > ( { } ) ; export const SideEffectContextProvider = ( { children , value } ) => ( < SideEffectContext . Provider value = { value } > { children } < /SideEffectContext.Provider>); example export const useSaveModifiers = ( { onSuccess , onFailure , transform , } ) => { const onSuccessRef = useRef ( onSuccess ) ; const setOnSuccess = onSuccess => { onSuccessRef . current = response => { onSuccessRef . current = undefined ; return onSuccess ( response ) ; } ; } ; const onFailureRef = useRef ( onFailure ) ; const setOnFailure = onFailure => { onFailureRef . current = error => { onFailureRef . current = undefined ; return onFailure ( error ) ; } ; } ; const transformRef = useRef ( transform ) ; const setTransform = transform => { transformRef . current = data => { transformRef . current = undefined ; return transform ( data ) ; } ; } ; return { onSuccessRef , setOnSuccess , onFailureRef , setOnFailure , transformRef , setTransform , } ; } ; export type SetOnSuccess = ( onSuccess : OnSuccess ) => void ; export type SetOnFailure = ( onFailure : OnFailure ) => void ; export type TransformData = ( data : any ) => any | Promise < any > ; export type SetTransformData = ( transform : TransformData ) => void ; export interface SideEffectContextValue { setOnSuccess ? : SetOnSuccess ; setOnFailure ? : SetOnFailure ; setTransform ? : SetTransformData ; } export interface SideEffectContextOptions { onSuccess ? : OnSuccess ; onFailure ? : OnFailure ; transform ? : TransformData ; }