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-state-bridge

v1.0.10

Published

Global state management library with data and API interactions

Downloads

4

Readme

npm version code style: prettier

react-state-bridge

A custom React hook to simplify all api calls between frontend and backend and manage the various main states that result.

Table of contents

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Installation

To install and set up the library, run:

$ npm i react-state-bridge

Usage

To use the useDataFetch hook, follow these steps:

  1. Import the hook in your component file:
import useDataFetch from 'react-state-bridge';
  1. Call the useDataFetch hook in your functional component, providing the URL for the GET request as the first parameter. You can also pass optional parameters and an optional token for authorization:
type YourDataType = {
  // ... Your data type logic ...
}

type YourDataType2 = {
    // ... Your data type logic ...
}

const MyComponent = () => {
  const url = 'https://api.example.com/data'
  const url2 = 'https://api.example2.com/data'

  const token = env.YOUR_TOKEN

  const params = {
     yourparams1: 'params1',
     yourparams2: 'params2',
     yourparams3: 'params3'
  }
  const params2 = {
     yourparams4: 'params4',
     yourparams5: 'params5',
     yourparams6: 'params6'
  }

  const headers = {
    // ... Your headers ...
  }
  const headers2 = {
    // ... Your headers ...
  }

  const fetchResult = useDataFetch<YourDataType[]>(url, params, token, headers)
   const fetchResult2 = useDataFetch<YourDataType2[]>(url2, params2, token, headers2);

// Using data for the first call

  const { data: data, isLoading: isLoading, error: error, messageSucess: messageSucess } = fetchResult

// Use of data for a possible second call

  const { data: data2, isLoading: isLoading2, error: error2, messageSucess: messageSucess2 } = fetchResult2

  // ... Your component logic ...
}
  1. The hook returns an object with the following properties:
  • data: The fetched data from the API.
  • isLoading: A boolean indicating whether the data is currently being fetched.
  • error: An error message in case the request fails.
  • messageSuccess: A success message if the request is successful.

Exemple

import React from 'react';
import {useDataFetch} from 'react-state-bridge';

type PostData = {
  userId: number;
  id: number;
  title: string;
  body: string;
}

const MyComponent: React.FC = () => {
  const apiUrl = 'https://jsonplaceholder.typicode.com/posts';
  const yourAuthToken = env.YOUR_TOKEN;
  const yourHeader = {
    'apiKey': 'your-key',
    'host': 'jsonplaceholder.typicode.com'
  }

  const fetchPostResult = useDataFetch<PostData[]>(url, null, yourAuthToken, yourHeader)

  const { data: postData, isLoading: isPostLoading, error: postError, messageSucess: postMessageSucess } = fetchPostResult

  return (
    <div>
      {isPostLoading && <p>Loading...</p>}
      {postData && (
        <div>
          {postData.map(post => (
            <div key={post.id}>
              <h2>{post.title}</h2>
              <p>{post.body}</p>
            </div>
          ))}
        </div>
      )}
      {error && <p>Error: {error}</p>}
      {postMessageSucess && <p>{messageSucess}</p>}
    </div>
  );
};

export default MyComponent;

API

useDateFetch

useDateFetch(url: AxiosRequestConfig['url'] = '', params?: AxiosRequestConfig['params'], token?: string, headers?: 
AxiosRequestConfig['headers'])

Authors

  • Lindow - react-state-bridge - Lindow

Issues

If you have a bug or suggestions for improvement, don't hesitate to report it, right here. issues