- 概念: 自定義hook是react中最新的組件代碼復用方案
注意點: hook必須以use開頭镇眷,并需要使用別的hook - 例子
1)useEffect
useEffect(() => {
fetch(`${apiUrl}/users`).then(async response => {
if (response.ok) {
setUsers(await response.json())
}
})
}, [])
改造后
export const useMount = (callback) => {
useEffect(() => {
callback();
}, []);
useMount(() => {
fetch(`${apiUrl}/users`).then(async response => {
if (response.ok) {
setUsers(await response.json())
}
})
})
- useDebounce
常用debounce
const debounce = (func, delay) => {
let timeout;
return (...param) => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(function() {
func(...param);
}, delay);
}
}
const log = debounce(() => console.log('call'), 5000)
log()
log()
log()
但在以下場景使用并不優(yōu)雅
const [param, setParam] = useState({
name: '',
personId: ''
})
useEffect(() => {
fetch(`${apiUrl}/projects?${qs.stringify(cleanObject(param))}`).then(async response => {
if (response.ok) {
setList(await response.json())
}
})
}, [param])
通過useDebounce來
export const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
// 每次在value變化以后,設置一個定時器
const timeout = setTimeout(() => setDebouncedValue(value), delay);
// 每次在上一個useEffect處理完以后再運行
return () => clearTimeout(timeout);
}, [value, delay]);
return debouncedValue;
};
const [param, setParam] = useState({
name: '',
personId: ''
})
const debouncedParam = useDebounce(param, 2000)
useEffect(() => {
fetch(`${apiUrl}/projects?${qs.stringify(cleanObject(debouncedParam))}`).then(async response => {
if (response.ok) {
setList(await response.json())
}
})
}, [debouncedParam])
- useArray
import { useArray, useMount } from "utils";
import React, { useState } from "react";
export const TsReactTest = () => {
const persons: { name: string; age: number }[] = [
{ name: "jack", age: 25 },
{ name: "ma", age: 22 },
];
const { value, clear, removeIndex, add } = useArray(persons);
return (
<div>
{value.map((person, index) => (
<div style={{ marginBottom: "30px" }}>
<span style={{ color: "red" }}>{index}</span>
<span>{person.name}</span>
<span>{person.age}</span>
</div>
))}
{/* 點擊以后清空列表*/}
<button style={{ marginBottom: "50px" }} onClick={() => clear()}>
clear
</button>
{/* 點擊以后刪除第一項*/}
<button onClick={() => removeIndex(0)}>remove 0</button>
{/* 點擊以后增加 john */}
<button onClick={() => add({ name: "john", age: 22 })}>add john</button>
</div>
);
export const useArray = <T>(initialArray: T[]) => {
const [value, setValue] = useState(initialArray);
return {
value,
setValue,
add: (item: T) => setValue([...value, item]),
clear: () => setValue([]),
removeIndex: (index: number) => {
const copy = [...value];
copy.splice(index, 1);
setValue(copy);
},
};
};