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

post-get-data-hook

v1.1.2

Published

Custom React hooks for performing GET and POST requests with support for loading, error handling, and success callbacks.

Downloads

7

Readme

Installation

You can install this package via npm:

npm i post-get-data-hook

Features

  • Flexible API Requests: Fetch and post data with optional headers and payloads.
  • Loading State Management: Automatically tracks loading states for both GET and POST requests.
  • Error Handling: Easily manage errors during data fetching or posting.
  • Data Transformation (GET only): Transform fetched data with a custom function.
  • Callbacks for Success and Error Handling: Provide custom behavior for successful and failed requests.

Usage: useGetDataFromServer

This hook is designed for making GET requests to fetch data from the server.

Example

import {useGetDataFromServer} from 'post-get-data-hook';

const MyComponent = () => {
    const { data, isLoading, isError, fetchData } = useGetDataFromServer();

    const handleFetch = () => {
        fetchData({
            API_URL: "https://api.example.com/data",
            HEADERS: { Authorization: "Bearer myToken" },
            structureApiData: (data) => data.items, // Optional transformation
            onGetReqSuccess: (finalData) => console.log("Success:", finalData),
            onError: (error) => console.error("Error:", error),
        });
    };

    if (isLoading) return <p>Loading...</p>;
    if (isError) return <p>Error occurred!</p>;

    return (
        <div>
            <button onClick={handleFetch}>Fetch Data</button>
            {data && data.map(item => (
                <p key={item.id}>{item.name}</p>
            ))}
        </div>
    );
};

Hook Parameters (GET)

  • API_URL (string): The URL of the API endpoint.
  • HEADERS (object, optional): Custom headers for the API request (default: {}).
  • onGetReqSuccess (function, optional): Callback function for handling successful data fetch.
  • onError (function, optional): Callback function for handling errors.
  • structureApiData (function, optional): Function to transform or structure the fetched data.

Return Values (GET)

  • data: The fetched API data, transformed if structureApiData is provided.
  • isLoading: A boolean value indicating if the request is currently in progress.
  • isError: A boolean value indicating if there was an error during the request.
  • fetchData: A function that triggers the API request with the provided parameters.

Usage: usePostDataToServer

This hook is designed for making POST requests to send data to the server.

Example

import {usePostDataToServer} from 'post-get-data-hook';

const MyComponent = () => {
    const { data, isLoading, isError, postData } = usePostDataToServer();

    const handlePost = () => {
        postData({
            API_URL: "https://api.example.com/data",
            HEADERS: { Authorization: "Bearer myToken" },
            body: { name: "John", age: 30 },
            onPostReqSuccess: (response) => console.log("Success:", response),
            onError: (error) => console.error("Error:", error),
        });
    };

    if (isLoading) return <p>Loading...</p>;
    if (isError) return <p>Error occurred!</p>;

    return (
        <div>
            <button onClick={handlePost}>Send Data</button>
            {data && <p>Response: {JSON.stringify(data)}</p>}
        </div>
    );
};

Hook Parameters (POST)

  • API_URL (string): The URL of the API endpoint.
  • HEADERS (object, optional): Custom headers for the API request (default: {}).
  • body (object): The data to be sent in the POST request.
  • onPostReqSuccess (function, optional): Callback function for handling a successful POST request.
  • onError (function, optional): Callback function for handling errors.

Return Values (POST)

  • data: The response data from the server.
  • isLoading: A boolean value indicating if the request is currently in progress.
  • isError: A boolean value indicating if there was an error during the request.
  • postData: A function that triggers the POST request with the provided parameters.

License

This project is licensed under the ISC License.