import { Record } from '../types'; import { MutationOptions, Mutation } from './useMutation'; /** * Get a callback to call the dataProvider.create() method, the result and the loading state. * * The return value updates according to the request state: * * - initial: [create, { loading: false, loaded: false }] * - start: [create, { loading: true, loaded: false }] * - success: [create, { data: [data from response], loading: false, loaded: true }] * - error: [create, { error: [error from response], loading: false, loaded: false }] * * @param resource The resource name, e.g. 'posts' * @param data The data to initialize the new record with, e.g. { title: 'hello, world' } * @param options Options object to pass to the dataProvider. May include side effects to be executed upon success or failure, e.g. { onSuccess: { refresh: true } } * * @returns The current request state. Destructure as [create, { data, error, loading, loaded }]. * * The create() function can be called in 3 different ways: * - with the same parameters as the useCreate() hook: create(resource, data, options) * - with the same syntax as useMutation: create({ resource, payload: { data } }, options) * - with no parameter (if they were already passed to useCreate()): create() * * @example // set params when calling the update callback * * import { useCreate } from '../app'; * * const LikeButton = ({ record }) => { * const like = { postId: record.id }; * const [create, { loading, error }] = useCreate(); * const handleClick = () => { * create('likes', like) * } * if (error) { return

ERROR

; } * return ; * }; * * @example // set params when calling the hook * * import { useCreate } from '../app'; * * const LikeButton = ({ record }) => { * const like = { postId: record.id }; * const [create, { loading, error }] = useCreate('likes', like); * if (error) { return

ERROR

; } * return ; * }; */ declare const useCreate: (resource?: string, data?: Partial, options?: MutationOptions) => UseCreateHookValue; declare type UseCreateHookValue = [ (resource?: string | Partial | Event, data?: Partial, options?: MutationOptions) => void | Promise, { data?: RecordType; total?: number; error?: any; loading: boolean; loaded: boolean; } ]; export default useCreate;