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

vwr-vuejs

v1.0.1

Published

VWR is an innovative data-fetching and caching library designed for Vue.js, serving as an alternative to SWR (Stale-While-Revalidate) commonly used in React.

Downloads

119

Readme

useVWR Hook

Overview

VWR is an innovative data-fetching and caching library designed for Vue.js, serving as an alternative to SWR (Stale-While-Revalidate) commonly used in React. VWR allows developers to seamlessly fetch, cache, and revalidate data in real-time, ensuring optimal user experiences with up-to-date content and minimal latency.
By employing smart caching strategies, VWR improves app performance by revalidating stale data in the background and delivering fresh results without disrupting the user interface. Built with Vue's reactivity in mind, it integrates smoothly with Vue components and provides a familiar API for effortless data management.

Installation

Ensure that your project is set up to use TypeScript and React. Install any necessary dependencies using:

npm install vwr-vuejs

Usage

Function Signature

const useVWR = <T>(
    key: string,
    fetcher: Function,
    options: VWROptions = {}
) => { ... }

Parameters

  • key: string: A unique identifier for the data request, ensuring proper caching and reuse.
  • fetcher: Function: A function that performs data fetching. It should ideally be asynchronous and return the data to be cached.
  • options: VWROptions (optional): Configuration options for the hook.

Returns

  • The hook instance that either reuses an existing request or initializes a new one.

Example

const fetchUserData = async (userId: string) => {
    const response = await fetch(`/api/users/${userId}`);
    return response.json();
};

const MyComponent = () => {
    const userId = '123';
    const userData = useVWR(userId, fetchUserData, {
        RevalidateInterval: 5000, // revalidate every 5 seconds
        ErrorCallback: (error) => console.error('Fetch failed:', error)
    });

    return (
        <div>
            {userData ? (
                <div>User Name: {userData.name}</div>
            ) : (
                <div>Loading...</div>
            )}
        </div>
    );
};

Configuration Options (VWROptions)

The VWROptions class provides additional options for customizing the behavior of the useVWR hook:

export default class VWROptions {
    RevalidateInterval?: number; // Time interval (in milliseconds) for revalidating the data.
    ErrorCallback?: Function; // Callback function to handle errors during the fetch.
}

Properties

  • RevalidateInterval: (optional) Specifies the interval in milliseconds for revalidating data. Useful for cases where data needs to be kept fresh.
  • ErrorCallback: (optional) A function to be called if an error occurs during the fetching process. This can be used for custom error handling or logging.

Explanation

  • The useVWR hook checks if a request associated with the key already exists using vwrExists(key).
  • If it exists, it reuses the existing data through reuseVWR<T>(key, fetcher).
  • If not, it initializes a new data request using initVWR<T>(key, fetcher, options).

Functions

vwrExists(key: string)

  • Checks if a request associated with the key exists in the cache.

reuseVWR<T>(key: string, fetcher: Function)

  • Reuses an existing data request, returning the cached data.

initVWR<T>(key: string, fetcher: Function, options: VWROptions)

  • Initializes a new data request and caches the result for future use.

Contributing

Contributions are welcome! If you find a bug or have suggestions for improvements, feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.