@shipengine/react-api 1.10.1 → 1.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# ShipEngine React Api
|
|
2
|
+
|
|
3
|
+
The `shipengine/react-api` library provides React bindings for the [shipengine/js-api](https://www.npmjs.com/package/@shipengine/js-api) client, with the addition of data sharing and cache management via [React Query](https://tanstack.com/query/latest) (hereon "RQ").
|
|
4
|
+
|
|
5
|
+
The client interface is exposed via hooks[^1] sharing a single RQ client instance; this allows multiple components to access data from the same ShipEngine API endpoint with a single network request.
|
|
6
|
+
|
|
7
|
+
For instance, if `ComponentA`, `ComponentB` and `ComponentC` separately invoke the `useListShipments` hook on a single render, the API requests will be deduped to a single `GET /v1/shipments`, with all three components sharing the data / loading / error / etc states from the RQ cache. Revalidating the request in one component similarly updates the others.
|
|
8
|
+
|
|
9
|
+
## Basic usage
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { ShipEngine, useListShipments } from "@shipengine/react-api";
|
|
13
|
+
|
|
14
|
+
const MyComponent = () => {
|
|
15
|
+
const { data, isLoading, error } = useListShipments();
|
|
16
|
+
|
|
17
|
+
if (isLoading) return <div>Loading...</div>;
|
|
18
|
+
if (error) return <div>{error.message}</div>;
|
|
19
|
+
|
|
20
|
+
console.log(data);
|
|
21
|
+
// > an object matching the response schema of https://shipengine.github.io/shipengine-openapi/#operation/list_shipments
|
|
22
|
+
|
|
23
|
+
return <div>Retrieved {data.shipments.length} shipments</div>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const MyApp = () => {
|
|
27
|
+
return (
|
|
28
|
+
<ShipEngine getToken={() => "your-platform-token"}>
|
|
29
|
+
<MyComponent />
|
|
30
|
+
<MyComponent />
|
|
31
|
+
<MyComponent />
|
|
32
|
+
{/* All three components will share the same data and only 1 request will be made */}
|
|
33
|
+
</ShipEngine>
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
```
|