Skip to content

Commit fcadd19

Browse files
committed
feature: updated documentation;
1 parent 8322038 commit fcadd19

File tree

3 files changed

+178
-12
lines changed

3 files changed

+178
-12
lines changed

CONTRIBUTORS.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Contributors
2+
3+
This is a list showing the GitHub usernames of all who have contributed to this open-source project! **Make sure to add yourself and submit a pull request if you've contributed.**
4+
5+
- [@UcheSylvester](https://github.com/uchesylvester)

README.md

+172-11
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,191 @@
1-
# react-query
1+
# React Query Pro
22

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 🔥😄🔥
44
5-
[![NPM](https://img.shields.io/npm/v/react-query.svg)](https://www.npmjs.com/package/react-query) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
5+
[![NPM](https://img.shields.io/npm/v/react-query-pro.svg)](https://www.npmjs.com/package/react-query-pro) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
66

77
## Install
88

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
1117
```
1218

1319
## Usage
1420

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+
1599
```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>
17109

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+
}
20142

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+
}
24166
}
167+
168+
return <button onClick={handleSubmit}>Post</button>
25169
}
170+
171+
export default MakeRequestExample
26172
```
27173

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+
28189
## License
29190

30191
MIT © [UcheSylvester](https://github.com/UcheSylvester)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "react-query",
2+
"name": "react-query-prop",
33
"version": "1.0.0",
44
"description": "A react package for handling network requests",
55
"author": "UcheSylvester",

0 commit comments

Comments
 (0)