0.2.3 • Published 4 years ago

react-api-hooks v0.2.3

Weekly downloads
44
License
MIT
Repository
github
Last release
4 years ago

React API Hooks

npm.io npm.io npm.io npm.io

React hooks to interact with an API from a stateless component using axios.

Features:

  • Uses axios and allows for complete request control
  • Works with stateless/functional components
  • Ability to filter and paginate API results
  • Ability to delay API calls while the user is typing
  • Works with infinite scroll components
  • Request auto-cancellation for concurrent requests or component unmount

Examples:

Installation:

npm i react-api-hooks -s

Basic Usage:

import { useAPI } from 'react-api-hooks';

const TestComponent = () => {
  const { data=[], error, isLoading } = useAPI(url);
  
 if (error){
   return <Error />
 }
        
 if (isLoading){
   return <Loading />
 }
    
  return (
      <div>
        {data.map(item => <span key={item.id}>{item.text}</span>)}
      </div>
    )
}

Advanced Usage

import { useAPI } from 'react-api-hooks';

const TestComponent = () => {
  const axiosConfig = {
    method: 'POST',
    data: { foo: 'bar' },
    params: { id: '14' }
  };
  const { response, error, isLoading } = useAPI(url, axiosConfig);
  
 if (error){
   return <Error />
 }
        
 if (isLoading){
   return <Loading />
 }
    
  return (
      <div>
        {response.data.map(item => <span key={item.id}>{item.text}</span>)}
      </div>
    )
}

Reference:

0.2.3

4 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.1.0

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

1.0.0

5 years ago