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

react-query-pro

v1.0.6

Published

Make asynchronous requests and manage data with ease using the patterns you are already familiar with

Downloads

5

Readme

React Query Pro

Make asynchronous requests and manage data with ease using the patterns you are already familiar with 🔥😄🔥

NPM JavaScript Style Guide

Install

npm install --save react-query-pro

or

yarn add react-query-pro

Usage

React query pro exposes two hooks useGetQuery and useQuery, a Query component, and a makeRequest function, all for handling and managing asynchronous request and data

useGetQuery

useGetQuery is used for fetching data by making a GET Request

import React from 'react'
import { useGetQuery } from 'react-query-pro'

export const UseGetQueryExample = () => {
  const { isLoading, data, error, retry } = useGetQuery({
    url: 'http://localhost:3000/posts/1',
    method: 'GET'
  })

  if (isLoading) return <p>loading...</p>

  if (error)
    return (
      <div>
        <p>An unexpected error occurred {error.message}</p>

        <button onClick={retry}>retry</button>
      </div>
    )

  return (
    <div>
      <p>author: {data.author.name}</p>
      <p>message: {data.message}</p>
    </div>
  )
}

export default UseGetQueryExample

useQuery

The useQuery is a special hook for performing any form of CRUD operation. It returns functions createQuery for triggering such requests

import React from 'react'
import { useQuery } from 'react-query-pro'

const UseQueryExample = () => {
  const { createQuery, isLoading, error, data } = useQuery({
    method: 'POST',
    url: 'http://localhost:3000/users/login'
  })

  const handleSubmit = async () => {
    try {
      const body = {
        // body of the request, in this case email and password for login
        email: '[email protected]',
        password: '12345'
      }

      const data = await createQuery(body)

      console.log({ data })
    } catch (error) {
      console.log({ error })
    }
  }

  return <button onClick={handleSubmit}>Send</button>
}

export default UseQueryExample

Query

The Query component uses the render prop pattern for managing asynchronous request. It is best used when making multiple request in a single component.

import React from 'react'
import { Query } from 'react-query-pro'

const QueryExample = () => {
  return (
    // For getting stories
    <Query url='http://localhost:3000/stories' method='GET'>
      {({ isLoading, error, data, retry }) => {
        if (isLoading) return <p>Fetching stories...</p>

        if (error)
          return (
            <>
              <p>An unexpected error occurred {error.message}</p>

              <button onClick={retry}>retry</button>
            </>
          )

        return <pre>{JSON.stringify(data)}</pre>
      }}
    </Query>

    // For getting posts
    <Query url='http://localhost:3000/posts' method='GET'>
      {({ isLoading, error, data, retry }) => {
        if (isLoading) return <p>Fetching user...</p>

        if (error)
          return (
            <>
              <p>An unexpected error occurred {error.message}</p>

              <button onClick={retry}>retry</button>
            </>
          )

        return <pre>{JSON.stringify(data)}</pre>
      }}
    </Query>
  )
}

export const QueryExample

makeRequest

Don't want to use hooks or the Query component? makeRequest is here for you 😃

import React from 'react'
const MakeRequestExample = () => {
import { makeRequest } from 'react-query-pro'

const MakeRequestExample = () => {
  const handleSubmit = async () => {
    try {
      const response = await makeRequest({
        method: 'POST',
        url: 'http://localhost:3000/posts',
        data: {
          post: 'Hello world'
        }
      })

      console.log({ response })
    } catch (error) {
      console.log({ error })
    }
  }

  return <button onClick={handleSubmit}>Post</button>
}

export default MakeRequestExample

Types

All relevant types are bundled and exported with the npm package

Contributing

we hope to make this package the first option for making network request, so you are always welcome to make it better by contributing.

  • Fork it!
  • Create your feature branch: git checkout -b feature-name
  • commit your changes: git commit -am 'Some commit message
  • Push to the branch: git push origin feature-name
  • Submit a pull request :muscle:
  • Add your username to the contributors' list 😄 🥰

License

MIT © UcheSylvester