Back to Hooks

RefreshOnWindowFocus

packages/hooks/src/useRequest/doc/refreshOnWindowFocus/refreshOnWindowFocus.en-US.md

3.9.71.2 KB
Original Source

RefreshOnWindowFocus

By setting options.refreshOnWindowFocus, the request will be refreshed when the browser is refocus and revisible.

tsx
const { data } = useRequest(getUsername, {
  refreshOnWindowFocus: true,
});

You can click outside the browser, and then click the current page to experience the effect (or hide the current page and redisplay). If the interval from the previous request is greater than 5000ms, it will be requested again.

<code src="./demo/refreshOnWindowFocus.tsx" />

API

Options

PropertyDescriptionTypeDefault
refreshOnWindowFocusWhether to re-initiate the request when the screen refocus or revisible.booleanfalse
focusTimespanRe-request interval, in millisecondsnumber5000

Remark

  • options.refreshOnWindowFocus, options.focusTimespan support dynamic changes.
  • Listen for browser events visibilitychange and focus.