npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

request-react-hooks

v4.1.3

Published

Hook for making asynchronous requests and subscribing to updates request state

Downloads

10

Readme

Hook to subscribe to request state updates.

useLazyRequest accepts an asyncRequest function that returns some value. As the asyncRequest function runs, requestState change.

It is free dependencies library, that easy in usages and does not required wrappers

Install

By using npm on yarn:

npm install request-react-hooks
yarn add request-react-hooks

Example usage:

Pass an asyncRequest function to the hook and subscribe to requestState updates:

import { useLazyRequest } from 'request-react-hooks'

const asyncRequest = async (parameter: number): Promise<string> => {
  await requestToBackend()
  return 'its result ' + String(parameter)
}

const MyAsyncComponent = () => {
  const [requestState, lazyRequest] = useLazyRequest(asyncRequest)

  useEffect(() => {
    lazyRequest(1111)
  }, [])

  const { loading, value, error } = requestState

  // will return on render:
  // { loading: false, value: null, error: null }
  // { loading: true, value: null, error: null }
  // { loading: false, value: 'its result 1111', error: null }
  console.log(requestState)

  if (loading) {
    return <p>Loading...</p>
  } else if (error) {
    return <p>Error!</p>
  } else {
    return <p>{value}</p>
  }
}

Returned types

Be sure to specify the types of parameters and return values for the asyncRequest, passed to the hook.

Otherwise, the compiler will not know what value is in the requestState and with what it will be possible to call lazyRequest

import {
  ActionCreators,
  LazyRequest,
  RequestState,
  UseLazyRequestResult,
} from 'request-react-hooks'

type Params = { par1: string; par2: string }
type Result = { result1: string; result2: number }

const asyncRequest = async (params: Params): Promise<Result> => {
  await requestToBackend()
  return { result1: params.par1, result2: params.par2 }
}

const initialState: Result<Result> = {
  value: { result1: 'initial_value_1', result2: 'initial_value_2' },
  error: null,
  loading: false,
}

// type UseLazyRequestResult<typeof asyncRequest> = [
//   RequestState<Result>,
//   LazyRequest<typeof asyncRequest>,
//   ActionCreators<Result>
// ]
const useLazyResult: UseLazyRequestResult<typeof asyncRequest> = useLazyRequest(
  asyncRequest,
  initialState,
)

// type RequestState<Result> = {
//   value: Result | null,
//   loading: boolean,
//   error: Record<string, any>
// }
const state: RequestState<Result> = useLazyResult[0]

// interface LazyRequest<typeof asyncRequest> {
//  (args: Parameters<typeof asyncRequest>): Promise<Result | undefined>
// }
const lazyRequest: LazyRequest<typeof asyncRequest> = useLazyResult[1]

// type ActionCreators<Result> = {
//   setValue: (value: Result | null) => void
//   setLoading: (loading: boolean) => void,
//   setError: (error: Record<string, any> | null) => void
//   clearState: () => void
// }
const actions: ActionCreators<Result> = useLazyResult[2]

Request exception handling

If the lazyRequest function fails, an error state will be written to the requestState

const asyncRequest = async (parameter: string): Promise<string> => {
  await requestToBackend()

  if (parameter === 'invalid parameter') {
    throw { text: 'passed invalid parameter', status: 1001 }
  }

  return 'its result ' + parameter
}

const [requestState, lazyRequest] = useLazyRequest(asyncRequest)

useEffect(() => {
  lazyRequest('invalid parameter')
}, [])

// will return on render:
// { loading: false, value: null, error: null }
// { loading: true, value: null, error: null }
// {
//   loading: false,
//   value: null,
//   error: {
//     text: 'passed invalid parameter',
//     status: 1001,
//   }
// }
console.log(requestState)

Use all possibilities of useLazyRequest

import { useCallback } from 'react'
import { RequestState, useLazyRequest } from 'request-react-hooks'

type Result = string

const request = async (par1: string, par2: string): Promise<Result> => {
  await requestToBackend()
  return 'its result ' + par1 + par2
}

const initialState: RequestState<Result> = {
  value: 'initial_value',
  error: null,
  loading: false,
}

const Component = () => {
  const [requestState, lazyRequest, actions] = useLazyRequest(
    request,
    initialState,
  )

  const { loading, value, error } = requestState

  const handleReload = useCallback(() => {
    lazyRequest('value_1', 'value_2')
  }, [lazyRequest])

  // After click on reload button, user will see the disabled button
  // and error message, because clear error in state will be after succes
  // execute request
  if (error) {
    return (
      <>
        <p>Error!</p>

        <button onClick={handleReload} disabled={loading}>
          upload again
        </button>
      </>
    )
  }

  if (loading) {
    return <p>Loading</p>
  }

  // This is the first thing the user sees
  // because we passed initial values in state.
  // After click on reload button, user will see loading message,
  // because handling of load case happens before that case is handled
  return (
    <>
      <p>{value}</p>

      <button onClick={handleReload}>clear state and reload</button>
    </>
  )
}

More examples usage