Skip to content

Commit 9ae1966

Browse files
committed
add useAPI
1 parent e42d0bf commit 9ae1966

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Signum.React/Scripts/Services.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ModelState } from './Signum.Entities'
22
import { GraphExplorer } from './Reflection'
3+
import * as React from 'react'
34

45
export interface AjaxOptions {
56
url: string;
@@ -383,3 +384,24 @@ export class AbortableRequest<Q, A> {
383384
}) as Promise<A>;
384385
}
385386
}
387+
388+
389+
export function useAPI<T>(defaultValue: T, key: ReadonlyArray<any> | undefined, makeCall: (signal: AbortSignal) => Promise<T>): T {
390+
391+
const [data, updateData] = React.useState<T>(defaultValue)
392+
393+
React.useEffect(() => {
394+
var abortController = new AbortController();
395+
396+
makeCall(abortController.signal)
397+
.then(result => updateData(result))
398+
.done();
399+
400+
return () => {
401+
abortController.abort();
402+
}
403+
}, key);
404+
405+
return data;
406+
407+
}

0 commit comments

Comments
 (0)