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 🙏

© 2025 – Pkg Stats / Ryan Hefner

use-sr

v0.1.1

Published

Before introducing this package, I would like to briefly explain my motivation and application scenarios for developing this package

Downloads

12

Readme

single_request

Before introducing this package, I would like to briefly explain my motivation and application scenarios for developing this package

Let's first take a look at the traditional React request data scenario

This is a pure display list, and everything is normal when requesting data rendering through Axios When we need him in multiple places, like this

A duplicate network request has occurred

So our first requirement arises to share data from the same request

But let's not rush to solve it for now and continue watching more scenes

Let's add some content Add an age increasing feature to each sub project And add a function to save and modify this list component So we now have this requirement When increasing age, the data is the same. We hope that the data for each list item is synchronized But this data is stored in a local state, and we do not want to frequently initiate network requests to change the data. Instead, local records are stored on the server when saved

Let's take a look at the current actual situation first

Unfortunately, the status of the components is separate, so there is no synchronization locally

To solve this problem, we may need to perform state improvement or use useContext

But we didn't want to do this, so I tried writing a hook to solve this problem

useSingleRequest

Let's try using the brand new hooks

The network request is intuitive, only one remains

Synchronous change of local state Kapture 2023-05-03 at 16 13 44

Overwrite local state with new request

Kapture 2023-05-03 at 16 18 49

Through these demonstrations, I believe you will have some doubts, such as how state management is carried out. I have combined this with Facebook's experimental state management library, Recoil, so if you want to use it, you need to import and encapsulate the components of RecoilRoot and root nodes like this

If you are already using Recoil, all you need to do is import the SRProvider like this

Now let's officially learn about the specific usage methods

UseSingleRequest receives three parameters The first parameter is the unique key currently requested to store data for the same key The second parameter is that your request function will be actively called The third parameter is the formatting program you provided, which will format the request after successfully obtaining the result And the final result obtained is data SetData is used to change the local state, just like setState There are several state related Boolean values isLoading isError When a request error occurs, the error will be automatically captured If you want to know specific information, you can receive another errorMessage RunRequest is the request function that you pass in to encapsulate various internal states. When called, the result will be assigned to the data So you noticed that there are two ways to update data One is to update setData locally One is runRequest requesting new data updates

There is also an optional fourth parameter

{ log?: boolean, refreshInterval?: number (ms) }