shadcn-ahooks
useRequest

RefreshDeps

The API RefreshDeps of useRequest.

RefreshDeps

By setting options.refreshDeps, useRequest will run refresh automatically when dependencies change, achieving the effect of Refresh (repeat the last request).

const [userId, setUserId] = useState('1');
const { data, run } = useRequest(() => getUserSchool(userId), {
  refreshDeps: [userId],
});

In the example code above, useRequest will execution when it is initialized and userId changes.

It is exactly the same with the following implementation

const [userId, setUserId] = useState('1');
const { data, refresh } = useRequest(() => getUserSchool(userId));

useEffect(() => {
  refresh();
}, [userId]);

Repeat last request

Custom refresh

API

Options

PropertyDescriptionTypeDefault
refreshDepsWhen the content of the array changes, trigger refresh.React.DependencyList[]
refreshDepsActionCustomize the request behavior during dependency refresh; this parameter is invoked when dependencies change.() => void-

Remark

  • If you set options.manual = true, both refreshDeps and refreshDepsAction are no longer effective, you need to trigger the request by run/runAsync.

On this page