1.2.0 • Published 8 months ago

@watchable-react/request v1.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

@watchable-react/request

Adds a utility to handle and cache requests in @watchable/store

Read the API Reference or the reference usages below, or browse the source on Github.

Getting Started

Install

npm install @watchable-react/request

Get handle on result and a function reference to re-load data in cache.

// with the store
const appStore: Store<AppState> = useAppStore();

// and the request function
const findUser = async (userName: string, email: string, role: string):Promise<User> => { 
  return await new Promise((resolve) => {
    let timeout = setTimeout(() => {
      clearTimeOut(timeout);
      resolve({
        name: "dummyUser",
        id: "123456789"
      });
    }, 1000)
  }) 
}


// You can use the hook like this:
const [result, refreshUser] = useRequest<User>(store, findUser, "nickname", "nickname@mail.org", "customer")

// Get request state and data from the result object like this:
const { isLoading, loadingState } = result;

if(loadingState === "LOADED") {
  const { data } = result 
}

// changing the params in the hook will retreive a different entity

// Reload the cache if needed:
refreshUser();
1.2.0

8 months ago

1.1.0

8 months ago

0.1.0

8 months ago