Custome 反应钩子与获取是循环的,因为钩子从组件接收参数



组件和钩子:

// 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]}
}

相关内容

  • 没有找到相关文章

最新更新