New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

use-api-handler

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-api-handler

A versatile hook for handling API requests in React applications.

latest
Source
npmnpm
Version
1.0.8
Version published
Maintainers
1
Created
Source

use-api-handler

A versatile hook for handling API requests in React applications.

npm version Build Status License

Table of Contents

Installation

npm install use-api-handler

Usage

import React from 'react';
import useApiHandler from 'use-api-handler';

function App() {
  const { data, loading, error, sendRequest } = useApiHandler('https://api.example.com/data');

  return (
    <div>
      {loading && <p>Loading...</p>}
      {error && <p>Error: {error.message}</p>}
      {data && <pre>{JSON.stringify(data, null, 2)}</pre>}
      <button onClick={() => sendRequest('GET')}>Fetch Data</button>
    </div>
  );
}

export default App;

Features

Simplifies API requests in React applications Pagination support with fetchPaginatedData function Caching of API responses using fetchCachedData Error handling and automatic retries with sendRetriableRequest Custom request headers and authentication support Clean cancellation and cleanup on unmounting

Examples

Check out the examples directory for more usage examples and integration scenarios.

API Reference

useApiHandler(initialUrl, initialData, debounceDelay) Parameters initialUrl: Initial API endpoint URL. initialData (optional): Initial data for the hook. Default: null. debounceDelay (optional): Delay in milliseconds for URL debouncing. Default: 300. Returns An object with the following properties and functions:

data: The fetched data. loading: A boolean indicating if the request is in progress. error: An error object if the request encountered an error. setUrl(url): Update the API endpoint URL. sendRequest(method, requestData, requestConfig): Send a custom API request. fetchPaginatedData(page, pageSize): Fetch paginated data. fetchCachedData(): Fetch cached data or perform a new request if not cached. sendAuthenticatedRequest(method, requestData, token): Send an authenticated API request. sendRetriableRequest(method, requestData, requestConfig, retries, delay): Send a retriable API request.

Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details on how to contribute to this project.

License

This project is licensed under the MIT License. See LICENSE for more information.

Keywords

react

FAQs

Package last updated on 11 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts