use-reaction
Version:
react modulized store manage framework
50 lines (49 loc) • 2.66 kB
TypeScript
export interface KV {
[k: string]: any;
}
export interface Model extends KV {
NAME?: string;
}
export declare type Action<M = Model> = (param: {
payload?: any;
store: Readonly<M>;
}) => void | Partial<M> | Promise<Partial<M>> | Promise<void>;
/** call this at the top line of your app to initialize, provide a 'true' param if you want to enable devtool, Note: it's better not enable devtool for your production mode */
export declare function useReaction(enableDev?: boolean, strict?: boolean): void;
/** call this to get the root Provider to wrap your app */
export declare const useProvider: () => any;
/**call this to retrive the store + action-trigger + resetModel-trigger of given model */
export declare function useModel<M extends Model = Model>(model: M): {
/**the store of this model */
store: M;
/**
* action trigger
* @param action the action-like function
* @param payload the payload which will pass to action-function
* @param showLoading whether showloading, possible value is 'model' | 'global' . default=undefined, 'global' means show global loading; and 'model' means only change the loading flag for this model
*/
doAction: (action: Action<M>, payload?: any, showLoading?: 'model' | 'global', loadingTip?: string) => Promise<Partial<M>> | Promise<void>;
/** a convenient trigger to execute freedom function in action queue, and optional trigger loading, but won't affect model data */
doFunction: (fn: Function, showLoading?: 'model' | 'global', loadingTip?: string) => any;
/**the function to reset model to it's initial state when you defined it */
resetModel: () => void;
};
export declare function useLoadingTip<M extends Model>(m?: M): {
loading: boolean;
tip?: string;
};
/**get the loading state of given model, if don't provide model param, then will return the global loading state */
export declare const useLoading: <M extends Model>(m?: M) => boolean;
/**
* NOT recommended to use, you'd better trigger loading by call doAction or doFunction
* This function might be usefull where need to mark global loading in non-UI section, eg. within fetch or axios call
* @param loading boolean
* @param tip loading tip, you can use this text to display the processing hint
* @param m set the loading(true/false) of specific model, if not provide, then set global loading
*/
export declare function setLoading(loading: boolean, tip?: string, m?: Model): void;
/**
* use this in your action function to just return data without modify model, won't trigger rerender
* @param data the data to return outter
*/
export declare const justBack: (data: any) => any;