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-swapi

v1.0.1

Published

A comprehensive library for accessing SWAPI information

Downloads

30

Readme

react-swapi

A custom React library with hooks for fetching data from the Star Wars API (SWAPI). https://swapi.tech

Installation

npm install react-swapi

Usage

The primary function provided by this library is useSwapi.

import { useSwapi } from "react-swapi";

// Inside your component
const { data, isLoading, error } = useSwapi(resource, options);

Parameters

  • resource: (string) The type of resource you want to fetch. For example: 'people', 'planets', etc (see SWAPI Documentation for resources).
  • options: (optional object) An object to include url params and/or a query string to refine results.

Return Values

  • data: The fetched data.
  • isLoading: A boolean indicating if the fetch request is ongoing.
  • error: Any error that occurred during the fetch.

Example

import { useSwapi } from "react-swapi";

function StarWarsCharacter() {
	const { data, isLoading, error } = useSwapi("people", { name: "skywalker" });

	if (isLoading) return <p>Loading...</p>;
	if (error) return <p>Error: {error.message}</p>;

	return <div>{/* Render your data here */}</div>;
}

Caching Mechanism

To enhance performance and reduce redundant network calls, react-swapi incorporates an internal caching mechanism. Here's how it works and how you can benefit from it:

How It Works

  1. Initial Request: The first time you fetch a particular resource or query, react-swapi will make a network request to the SWAPI.
  2. Storing Data: Once the data is retrieved, it's stored in localstorage with a timestamp.
  3. Subsequent Requests: If you attempt to fetch the same resource or query again, react-swapi will skip the network request and directly provide the data from the cache. This results in instantaneous data retrieval, reducing latency and conserving network resources. The cache will refresh on requests made after 1 hour from the initial storage.

Benefits

  • Faster Data Retrieval: Especially noticeable when re-fetching previously fetched data.
  • Reduced Network Load: Helps in scenarios with limited network availability or where minimizing requests is crucial.
  • Optimized Resource Usage: Conserves both client and server-side resources.

Things to Consider

  • Data Freshness: Caching is great for performance but be aware that if the data on the SWAPI changes after you've fetched it, the cache will still hold the old data until it's cleared or until the cache expires (if an expiration mechanism is implemented).
  • Manual Cache Management: In future versions, we may provide ways to manually clear the cache or set cache durations. Watch this space for updates! In the meantime, you can leverage the localstorage api to handle the cache for your own needs.

See our CONTRIBUTING.md to find out how you can help!