React API Hooks

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

npm i react-api-hooks -s
reacthooksAPIaxiosstateless

Basic Example

Basic usage of the useAPI hook to fetch a list of books from the Google Books API.

The data property provides the API response if the API request is successful.

The isLoading and error properties can be used to indicate the request status to the user.

Loading

const BasicExample = () => {
  const { data = [], error, isLoading } = useAPI(booksURL, { params: booksInitialParams });

  if (error) {
    return <Error error={error} />;
  }

  return isLoading ? <Loading /> : <GoogleBooksList data={data} />;
};

export default BasicExample;