Skip to main content

useReload

This hook returns a function to reload requests. It shares the context of LoadConfig with useLoad.

API

function useReload(
key: {},
options?: { force?: boolean }
): (params?: { force?: boolean }) => void;

Params:

  • key: The key set in useLoad.
  • options: Options object.
    • options.force: Force reload, ignore the staleTime option set in useLoad.

Results:

  • A function to reload all of the requests. It also accpets a force option.

Example

Number: Loading...
import React from "react";
import { useLoad, useReload } from "@lilib/hooks";

const getNumber = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(Math.random());
}, 1000);
});
};

function Component() {
const { data, loading } = useLoad(getNumber, [], {
key: "reload-key",
});

return <div>Number: {loading ? "Loading..." : data}</div>;
}

function Example() {
const reload = useReload("reload-key");

return (
<>
<Component />
<button onClick={() => reload()}>Reload</button>
</>
);
}

export default Example;