Package Exports
- api-client-react
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (api-client-react) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
api-client-react
Small library to simplify the connection with api in React.
With a simple higher-order component (HOC), you can get:
- The following props:
{ loading, error, data }
- The following prop:
apiClient
, with which you can interact with API.
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 = ({ loading, users, complete, error }) => {
if (loading) {
return <p>loading</p>;
}
if (error) {
return <p>Error</p>;
}
if (users) {
return (
<ol>
{users.map(b => (
<li key={b.name}>{b.name}</li>
))}
</ol>
);
}
return <strong>{complete}</strong>;
}
// Same object: https://github.com/axios/axios#axios-api
const apiSettings = {
method: "get",
url: "https://jsonplaceholder.typicode.com/users"
};
const mapApiProps = ({ loading, data, error }) => ({
loading,
users: data,
error,
complete: !loading && data && !error ? "true" : "false"
});
export const ExampleconnectApiClient = connectApiClient(apiSettings, mapApiProps)(ExampleconnectApiClientComponent);
NOTE: If you want to make the call in a method, it is also possible: View codesandbox.
Doc
Functions
function | params | description |
---|---|---|
apiClient.fetch({config}) |
Same object: https://github.com/axios/axios#axios-api | Execute axios fetch with the configuration provided |
Render props
prop | types | default value | description |
---|---|---|---|
error |
{ elementKey: String[], ... } |
{} |
Api error: 404, 503, ... |
data |
{ element: String, ... } |
{} |
Data response |
loading |
boolean |
true |
Only true during call response period. |
apiClient |
object |
{ fetch: function } |
Method that allows you to make the call to api. |
MIT License.