组件和钩子:
// FulfillmentOrders.js
export default function FulfillmentOrders (props) {
const { list, fetchList } = useFetchSOList(1)
return (...)
}
// useFetchSOList.js
export default status => {
const initialSearch = {
status,
current: 1,
pageSize: 10,
total: 0,
}
const [list, setList] = useState()
const fetchList = useCallback((search = {}) => {
const defaultSearch = search.reset ? initialSearch : search
const {
current,
pageSize,
status,
} = defaultSearch
axios.post().then().catch().finally()
}, [initialSearch]}
}
然后获取是无穷大循环,我尝试从initialSearch
中删除 status
属性,它仍在循环。
我想知道为什么,这让我感到困惑。
问完问题后,我发现我知道了这个问题的关键,因为当前initialSearch
总是与pre值不同,所以用useMemo
来缓存值。
const getInitialSearch = status => ({
status,
current: 1,
pageSize: 10,
total: 0,
})
// useFetchSOList.js
export default status => {
const initialSearch = useMemo(() => getInitialSearch(status), [status])
const [list, setList] = useState()
const fetchList = useCallback((search = {}) => {
const defaultSearch = search.reset ? initialSearch : search
const {
current,
pageSize,
status,
} = defaultSearch
axios.post().then().catch().finally()
}, [initialSearch]}
}