From 3d606b86cd8833a19b53ad99d5264b29cde7550f Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Wed, 4 Nov 2020 13:10:52 +0300 Subject: [PATCH] doc: add small example to readme doc: add save to deps flag Closes #1 --- README.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0afd21b..2bf64b4 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,58 @@ Minimalistic REST API client for React inspired by Apollo. # Installation ```bash -npm install react-rest-request +npm install react-rest-request --save +``` + + +# Usage + +```typescript +import React from 'react'; +import ReactDOM from 'react-dom'; +import { Endpoint, Method, useRequest, RequestProvider } from 'react-rest-request'; + +const BASE_API_URL = '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() { + const [movies, { data, loading }] = useRequest(MoviesEndpoint); + + React.useEffect( + () => { + movies(); + }, + [movies] + ); + + return !data ? ( +
{ loading ? 'Loading...' : 'Something went wrong' }
+ ) : ( + + ); +} + +ReactDOM.render( + + + , + document.getElementById('root'), +); ```