This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
react-rest-request/README.md

59 lines
1.1 KiB
Markdown
Raw Normal View History

# react-rest-request
Minimalistic REST API client for React inspired by Apollo.
# Installation
```bash
npm install react-rest-request --save
```
# Usage
```typescript
import React from 'react';
import ReactDOM from 'react-dom';
import { Client, Endpoint, Method, useRequest, RequestProvider } from 'react-rest-request';
2020-11-05 22:16:19 +03:00
const client = new Client({
baseUrl: 'https://sampleapis.com/movies/api',
});
type Movie = Readonly<{
id: number;
title: string;
posterURL: string;
imdbId: string;
}>
const MoviesEndpoint: Endpoint = {
method: Method.GET,
url: '/action-adventure',
};
type MoviesResponse = Movie[];
function App() {
2020-11-06 01:14:56 +03:00
const { data, loading } = useRequest<MoviesResponse>(MoviesEndpoint);
return !data ? (
<div>{ loading ? 'Loading...' : 'Something went wrong' }</div>
) : (
<ul>
{data.map(movie => (
<li key={movie.id}>{movie.title}</li>
))}
</ul>
);
}
ReactDOM.render(
<RequestProvider client={client}>
<App />
</RequestProvider>,
document.getElementById('root'),
);
```