2.1.0 • Published 5 years ago

api-client-react v2.1.0

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

api-client-react

Small library to simplify the connection with api in React.

NPM dependencies license NPM downloads code style: prettier Last commit PRs welcome minzipped size

With a simple higher-order component (HOC), you can get: 1. The following props for section: { loading, error, data, complete } 2. The following prop: apiClient, with which you can interact with API.

  • Note: Each API call is stored in a section

Examples

Getting Started

Installation

You can install with NPM: api-client-react

npm i --save api-client-react

Create your component and connect

import { connectApiClient } from "api-client-react";

const ExampleconnectApiClientComponent = ({ users }) => {
  if (users.loading) {
    return <p>loading</p>;
  }

  if (users.error) {
    return <p>Error</p>;
  }

  if (users.data) {
    return (
      <ol>
        {users.data.map(b => (
          <li key={b.name}>{b.name}</li>
        ))}
      </ol>
    );
  }
}


const config = {
  apiConfig: { // Same object: https://github.com/axios/axios#axios-api
    method: "get",
    url: "https://jsonplaceholder.typicode.com/users"
  },
  section: "users" // Reference section
};

const componentSections = ["users"];

export const ExampleconnectApiClient = connectApiClient(componentSections, config)(ExampleconnectApiClientComponent);

NOTE: If you want to make the call in a method, it is also possible: View codesandbox. Note: Important to send properties apiConfig and section in config property.

Doc

Functions

function (render props)paramsdescription
apiClient.fetch( apiConfigObject, "sectionExample" )apiConfigObject: Same object: https://github.com/axios/axios#axios-apiExecute axios fetch with the configuration provided And associate the response to your section

Render props

You will receive a props for each section that is an object composed of:

proptypesdefault valuedescription
errorerrorfalseApi error
dataresultData response
loadingbooleanfalseOnly true during call response period.
completebooleanfalseOnly true when api call is finished.

MIT License.


2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago