diff --git a/Signum.React/Scripts/Services.ts b/Signum.React/Scripts/Services.ts index b0ab37ee57..2fb47f8798 100644 --- a/Signum.React/Scripts/Services.ts +++ b/Signum.React/Scripts/Services.ts @@ -1,5 +1,6 @@ import { ModelState } from './Signum.Entities' import { GraphExplorer } from './Reflection' +import * as React from 'react' export interface AjaxOptions { url: string; @@ -383,3 +384,24 @@ export class AbortableRequest { }) as Promise; } } + + +export function useAPI(defaultValue: T, key: ReadonlyArray | undefined, makeCall: (signal: AbortSignal) => Promise): T { + + const [data, updateData] = React.useState(defaultValue) + + React.useEffect(() => { + var abortController = new AbortController(); + + makeCall(abortController.signal) + .then(result => updateData(result)) + .done(); + + return () => { + abortController.abort(); + } + }, key); + + return data; + +}