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 🙏

© 2026 – Pkg Stats / Ryan Hefner

startfetch

v8.9.12

Published

A library for fethcing API calls and their states

Downloads

45

Readme

StartFetch Library

Overview

StartFetch is a React library that provides a context and hooks for managing API calls and their states (loading, error, and data). It includes a wrapping component that simplifies the process of fetching data from APIs.

Installation

To install the StartFetch library, you can use npm or yarn:

npm install startfetch

or

yarn add startfetch

Usage

WrappingComponent

The WrappingComponent is a context provider that wraps your application or component tree. It provides the API context to its children.

import React from 'react';
import { WrappingComponent } from 'startfetch';

const App = () => (
  <WrappingComponent>
    <YourComponent />
  </WrappingComponent>
);

export default App;

useFetch Hook

The useFetch hook allows you to access the API context within your components.

import React from 'react';
import { useFetch } from 'startfetch';

const YourComponent = () => {
  const { response, loading, error } = useFetch('https://api.example.com/data', 'GET');

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <div>
      <pre>{JSON.stringify(response, null, 2)}</pre>
    </div>
  );
};

export default YourComponent;

StartFetch Library

[Previous sections remain unchanged...]

useFetchIf Hook

The useFetchIf hook allows you to conditionally fetch data based on a condition. Here are examples for different HTTP methods:

GET Request

import React, { useState } from 'react';
import { useFetchIf } from 'startfetch';

const GetComponent = () => {
  const [startFetching, setStartFetching] = useState(false);
  const { response, loading, error } = useFetchIf(
    "https://sw-api.starnavi.io/planets",
    "GET",
    null,
    startFetching
  );

  const handleClick = () => {
    setStartFetching(true);
  };

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <div>
      <button onClick={handleClick}>Fetch Data</button>
      <pre>{JSON.stringify(response, null, 2)}</pre>
    </div>
  );
};

POST Request

import React, { useState } from 'react';
import { useFetchIf } from 'startfetch';

const PostComponent = () => {
  const [shouldFetch, setShouldFetch] = useState(false);
  const requestBody = {
    title: 'New Item',
    description: 'Description here'
  };

  const { response, loading, error } = useFetchIf(
    'https://api.example.com/items',
    'POST',
    shouldFetch,
    requestBody
  );

  const handleSubmit = () => {
    setShouldFetch(true);
  };

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <div>
      <button onClick={handleSubmit}>Create Item</button>
      {response && <div>Successfully created!</div>}
    </div>
  );
};

PUT Request

import React, { useState } from 'react';
import { useFetchIf } from 'startfetch';

const PutComponent = () => {
  const [shouldFetch, setShouldFetch] = useState(false);
  const requestBody = {
    id: 1,
    title: 'Updated Item',
    description: 'Updated description'
  };

  const { response, loading, error } = useFetchIf(
    'https://api.example.com/items/1',
    'PUT',
    shouldFetch,
    requestBody
  );

  const handleUpdate = () => {
    setShouldFetch(true);
  };

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <div>
      <button onClick={handleUpdate}>Update Item</button>
      {response && <div>Successfully updated!</div>}
    </div>
  );
};

DELETE Request

import React, { useState } from 'react';
import { useFetchIf } from 'startfetch';

const DeleteComponent = () => {
  const [shouldFetch, setShouldFetch] = useState(false);
  
  const { response, loading, error } = useFetchIf(
    'https://api.example.com/items/1',
    'DELETE',
    shouldFetch
  );

  const handleDelete = () => {
    setShouldFetch(true);
  };

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <div>
      <button onClick={handleDelete}>Delete Item</button>
      {response && <div>Successfully deleted!</div>}
    </div>
  );
};

API

WrappingComponent Props

  • children: The components that will have access to the API context.

Hook Return Values

Both useFetch and useFetchIf hooks provide the following values:

  • response: The data received from the API.
  • loading: A boolean indicating if the request is currently in progress.
  • error: An error message if the request failed.

useFetchIf Parameters

The useFetchIf hook accepts the following parameters:

  • url (string): The API endpoint URL
  • method (string): The HTTP method ('GET', 'POST', 'PUT', 'DELETE')
  • condition (boolean): Whether to execute the request
  • body (optional object): The request body for POST and PUT requests

License

This project is licensed under the MIT License.