mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-07 14:39:25 +08:00
* fix: add license code * fix: bug * fix: bug * fix: upgrade * fix: improve * chore: add copyright information to the file header * fix: d.ts bug * fix: bug * fix: e2e bug * fix: merge main --------- Co-authored-by: chenos <chenlinxh@gmail.com>
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
/**
|
|
* This file is part of the NocoBase (R) project.
|
|
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
* Authors: NocoBase Team.
|
|
*
|
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
*/
|
|
|
|
import { Result } from 'ahooks/es/useRequest/src/types';
|
|
import React, { createContext, useContext } from 'react';
|
|
import { useRequest } from '../api-client';
|
|
|
|
export const AsyncDataContext = createContext<Result<any, any> & { state?: any; setState?: any }>(null);
|
|
AsyncDataContext.displayName = 'AsyncDataContext';
|
|
|
|
export interface AsyncDataProviderProps {
|
|
value?: any;
|
|
request?: any;
|
|
uid?: string;
|
|
onSuccess?: (data, params) => void;
|
|
}
|
|
|
|
export const AsyncDataProvider: React.FC<AsyncDataProviderProps> = (props) => {
|
|
const { value, request, children, ...others } = props;
|
|
const result = useRequest(request, { ...others });
|
|
if (value) {
|
|
return <AsyncDataContext.Provider value={value}>{children}</AsyncDataContext.Provider>;
|
|
}
|
|
return <AsyncDataContext.Provider value={result}>{children}</AsyncDataContext.Provider>;
|
|
};
|
|
|
|
export const useAsyncData = () => {
|
|
return useContext(AsyncDataContext);
|
|
};
|