YuJian920
3 years ago
10 changed files with 162 additions and 49 deletions
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
import { useState } from "react"; |
||||
|
||||
interface State<D> { |
||||
error: Error | null; |
||||
data: D | null; |
||||
stat: "idle" | "loading" | "error" | "success"; |
||||
} |
||||
|
||||
const defaultInitialState: State<null> = { |
||||
stat: "idle", |
||||
data: null, |
||||
error: null, |
||||
}; |
||||
|
||||
const useAsync = <D>(initialState?: State<D>) => { |
||||
const [state, setState] = useState<State<D>>({ |
||||
...defaultInitialState, |
||||
...initialState, |
||||
}); |
||||
|
||||
const setData = (data: D) => setState({ data, stat: "success", error: null }); |
||||
const setError = (error: Error) => |
||||
setState({ data: null, stat: "error", error }); |
||||
|
||||
const run = (promise: Promise<D>) => { |
||||
if (!promise || !promise.then) throw new Error("请传入 Promise 数据类型"); |
||||
setState({ ...state, stat: "loading" }); |
||||
return promise |
||||
.then((data) => { |
||||
setData(data); |
||||
return data; |
||||
}) |
||||
.catch((error) => { |
||||
setError(error); |
||||
return error; |
||||
}); |
||||
}; |
||||
|
||||
return { |
||||
isIdle: state.stat === "idle", |
||||
isLoading: state.stat === "loading", |
||||
isError: state.stat === "error", |
||||
isSuccess: state.stat === "success", |
||||
run, |
||||
setData, |
||||
setError, |
||||
...state, |
||||
}; |
||||
}; |
||||
|
||||
export default useAsync; |
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
import { useState, useEffect } from "react"; |
||||
|
||||
/** |
||||
* 防抖 hook |
||||
* @param value |
||||
* @param delay |
||||
* @returns |
||||
*/ |
||||
const useDebounce = <T>(value: T, delay: number) => { |
||||
const [debounceParams, setDebounceParams] = useState(value); |
||||
useEffect(() => { |
||||
const timeout = setTimeout(() => setDebounceParams(value), delay); |
||||
return () => clearTimeout(timeout); |
||||
}, [value, delay]); |
||||
|
||||
return debounceParams; |
||||
}; |
||||
|
||||
export default useDebounce; |
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
import { useEffect } from "react"; |
||||
|
||||
const useMount = (callback: () => void) => { |
||||
useEffect(() => { |
||||
callback(); |
||||
}, []); |
||||
}; |
||||
|
||||
export default useMount; |
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
import { useEffect } from "react"; |
||||
import { cleanObject } from "../utils"; |
||||
import { useRequest } from "../utils/request"; |
||||
import useAsync from "./useAsync"; |
||||
|
||||
interface Project { |
||||
id: string; |
||||
name: string; |
||||
personId: string; |
||||
pin: boolean; |
||||
organization: string; |
||||
created: number; |
||||
} |
||||
|
||||
const useProjects = (param?: Partial<Project>) => { |
||||
const request = useRequest(); |
||||
const { run, ...result } = useAsync<Project[]>(); |
||||
|
||||
useEffect(() => { |
||||
run(request("/projects", { data: cleanObject(param || {}) })); |
||||
}, [param]); |
||||
|
||||
return result; |
||||
}; |
||||
|
||||
export default useProjects; |
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
import { useEffect } from "react"; |
||||
import { cleanObject } from "../utils"; |
||||
import { useRequest } from "../utils/request"; |
||||
import useAsync from "./useAsync"; |
||||
|
||||
interface User { |
||||
id: string; |
||||
name: string; |
||||
email: string; |
||||
title: string; |
||||
token: string; |
||||
organization: string; |
||||
} |
||||
|
||||
const useUser = (param?: Partial<User>) => { |
||||
const request = useRequest(); |
||||
const { run, ...result } = useAsync<User[]>(); |
||||
|
||||
useEffect(() => { |
||||
run(request("/users", { data: cleanObject(param || {}) })); |
||||
}, [param]); |
||||
|
||||
return result; |
||||
}; |
||||
|
||||
export default useUser; |
@ -1 +0,0 @@
@@ -1 +0,0 @@
|
||||
export const baseURL = 'http://localhost:3001' |
Loading…
Reference in new issue