|
1 |
| -# react-query |
| 1 | +# React Query Pro |
2 | 2 |
|
3 |
| -> A react package for handling network requests |
| 3 | +> Make asynchronous requests and manage data with ease using the patterns you are already familiar with 🔥😄🔥 |
4 | 4 |
|
5 |
| -[](https://www.npmjs.com/package/react-query) [](https://standardjs.com) |
| 5 | +[](https://www.npmjs.com/package/react-query-pro) [](https://standardjs.com) |
6 | 6 |
|
7 | 7 | ## Install
|
8 | 8 |
|
9 |
| -```bash |
10 |
| -npm install --save react-query |
| 9 | +``` |
| 10 | +npm install --save react-query-pro |
| 11 | +``` |
| 12 | + |
| 13 | +or |
| 14 | + |
| 15 | +``` |
| 16 | +yarn add react-query-pro |
11 | 17 | ```
|
12 | 18 |
|
13 | 19 | ## Usage
|
14 | 20 |
|
| 21 | +React query pro exposes two hooks `useGetQuery` and `useQuery`, a `Query` component, and a `makeRequest` function, all for handling and managing asynchronous request and data |
| 22 | + |
| 23 | +#### useGetQuery |
| 24 | + |
| 25 | +`useGetQuery` is used for fetching data by making a `GET` Request |
| 26 | + |
| 27 | +```tsx |
| 28 | +import React from 'react' |
| 29 | +import { useGetQuery } from 'react-query-pro' |
| 30 | + |
| 31 | +export const App = () => { |
| 32 | + const { isLoading, data, error, retry } = useGetQuery({ |
| 33 | + url: 'http://localhost:3000/posts/1', |
| 34 | + method: 'GET' |
| 35 | + }) |
| 36 | + |
| 37 | + if (isLoading) return <p>loading...</p> |
| 38 | + |
| 39 | + if (error) |
| 40 | + return ( |
| 41 | + <div> |
| 42 | + <p>An unexpected error occurred {error.message}</p> |
| 43 | + |
| 44 | + <button onClick={retry}>retry</button> |
| 45 | + </div> |
| 46 | + ) |
| 47 | + |
| 48 | + return ( |
| 49 | + <div> |
| 50 | + <p>author: {data.author.name}</p> |
| 51 | + <p>message: {data.message}</p> |
| 52 | + </div> |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +export default App |
| 57 | +``` |
| 58 | + |
| 59 | +#### useQuery |
| 60 | + |
| 61 | +The `useQuery` is a special hook for performing any form of `CRUD` operation. It returns functions `createQuery` for triggering such requests |
| 62 | + |
| 63 | +```tsx |
| 64 | +import React from 'react' |
| 65 | +import { useQuery } from 'react-query-pro' |
| 66 | + |
| 67 | +const App = () => { |
| 68 | + const { createQuery, isLoading, error, data } = useQuery({ |
| 69 | + method: 'POST', |
| 70 | + url: 'http://localhost:3000/users/login' |
| 71 | + }) |
| 72 | + |
| 73 | + const handleSubmit = async () => { |
| 74 | + try { |
| 75 | + const body = { |
| 76 | + // body of the request, in this case email and password for login |
| 77 | + |
| 78 | + password: '12345' |
| 79 | + } |
| 80 | + |
| 81 | + const data = await createQuery(body) |
| 82 | + |
| 83 | + console.log({ data }) |
| 84 | + } catch (error) { |
| 85 | + console.log({ error }) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return <button onClick={handleSubmit}>Send</button> |
| 90 | +} |
| 91 | + |
| 92 | +export default App |
| 93 | +``` |
| 94 | + |
| 95 | +#### Query |
| 96 | + |
| 97 | +The `Query` component uses the render prop pattern for making asynchronous request. It is best used when making multiple request in a single component. |
| 98 | + |
15 | 99 | ```tsx
|
16 |
| -import React, { Component } from 'react' |
| 100 | +import React from 'react' |
| 101 | +import { Query } from 'react-query-pro' |
| 102 | + |
| 103 | +const App = () => { |
| 104 | + return ( |
| 105 | + // For getting stories |
| 106 | + <Query url='http://localhost:3000/stories' method='GET'> |
| 107 | + {({ isLoading, error, data, retry }) => { |
| 108 | + if (isLoading) return <p>Fetching stories...</p> |
17 | 109 |
|
18 |
| -import MyComponent from 'react-query' |
19 |
| -import 'react-query/dist/index.css' |
| 110 | + if (error) |
| 111 | + return ( |
| 112 | + <> |
| 113 | + <p>An unexpected error occurred {error.message}</p> |
| 114 | + |
| 115 | + <button onClick={retry}>retry</button> |
| 116 | + </> |
| 117 | + ) |
| 118 | + |
| 119 | + return <pre>{JSON.stringify(data)}</pre> |
| 120 | + }} |
| 121 | + </Query> |
| 122 | + |
| 123 | + // For getting posts |
| 124 | + <Query url='http://localhost:3000/posts' method='GET'> |
| 125 | + {({ isLoading, error, data, retry }) => { |
| 126 | + if (isLoading) return <p>Fetching user...</p> |
| 127 | + |
| 128 | + if (error) |
| 129 | + return ( |
| 130 | + <> |
| 131 | + <p>An unexpected error occurred {error.message}</p> |
| 132 | + |
| 133 | + <button onClick={retry}>retry</button> |
| 134 | + </> |
| 135 | + ) |
| 136 | + |
| 137 | + return <pre>{JSON.stringify(data)}</pre> |
| 138 | + }} |
| 139 | + </Query> |
| 140 | + ) |
| 141 | +} |
20 | 142 |
|
21 |
| -class Example extends Component { |
22 |
| - render() { |
23 |
| - return <MyComponent /> |
| 143 | +export const App |
| 144 | +``` |
| 145 | + |
| 146 | +#### makeRequest |
| 147 | + |
| 148 | +`makeRequest` is a special utility function for making request when you don't want to use any of the `hooks` or the `Query` component |
| 149 | + |
| 150 | +```tsx |
| 151 | +const MakeRequestExample = () => { |
| 152 | + const handleSubmit = async () => { |
| 153 | + try { |
| 154 | + const response = await makeRequest({ |
| 155 | + method: 'POST', |
| 156 | + url: 'http://localhost:3000/posts', |
| 157 | + data: { |
| 158 | + post: 'Hello world' |
| 159 | + } |
| 160 | + }) |
| 161 | + |
| 162 | + console.log({ response }) |
| 163 | + } catch (error) { |
| 164 | + console.log({ error }) |
| 165 | + } |
24 | 166 | }
|
| 167 | + |
| 168 | + return <button onClick={handleSubmit}>Post</button> |
25 | 169 | }
|
| 170 | + |
| 171 | +export default MakeRequestExample |
26 | 172 | ```
|
27 | 173 |
|
| 174 | +### Types |
| 175 | + |
| 176 | +All relevant types are bundled and exported with the npm package |
| 177 | + |
| 178 | +## Contributing |
| 179 | + |
| 180 | +we hope to make this package the first option for making network request, so you are always welcome to make it better by contributing. |
| 181 | + |
| 182 | +- Fork it! |
| 183 | +- Create your feature branch: `git checkout -b feature-name` |
| 184 | +- commit your changes: `git commit -am 'Some commit message` |
| 185 | +- Push to the branch: `git push origin feature-name` |
| 186 | +- Submit a pull request :muscle: |
| 187 | +- Add your username to the [contributors' list](CONTRIBUTORS.md) 😄 🥰 |
| 188 | + |
28 | 189 | ## License
|
29 | 190 |
|
30 | 191 | MIT © [UcheSylvester](https://github.com/UcheSylvester)
|
0 commit comments