Skip to content

Commit a687cc5

Browse files
committed
Adding useFetch hook
1 parent ff7ec6e commit a687cc5

File tree

6 files changed

+13514
-32
lines changed

6 files changed

+13514
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/node_modules
66
/umd
77
npm-debug.log*
8+
.idea

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
# useFetch
22

3-
[![Travis][build-badge]][build]
4-
[![npm package][npm-badge]][npm]
5-
[![Coveralls][coveralls-badge]][coveralls]
3+
A quick and easy hook for using Fetch with React.
64

7-
Describe useFetch here.
5+
## Install
86

9-
[build-badge]: https://img.shields.io/travis/user/repo/master.png?style=flat-square
10-
[build]: https://travis-ci.org/user/repo
7+
`npm install usefetch`
118

12-
[npm-badge]: https://img.shields.io/npm/v/npm-package.png?style=flat-square
13-
[npm]: https://www.npmjs.org/package/npm-package
9+
## Usage
10+
11+
```jsx
12+
import useFetch from 'usefetch'
13+
14+
const Example = () => {
15+
const {
16+
isLoading,
17+
error,
18+
data
19+
} = useFetch('https://get.geojs.io/v1/ip/country.json?ip=8.8.8.8')
20+
21+
if (isLoading) {
22+
return <p>Loading.....</p>
23+
}
24+
25+
if (error) {
26+
return <p>{JSON.stringify(error)}</p>
27+
}
28+
29+
return (
30+
<p>
31+
{JSON.stringify(data)}
32+
</p>
33+
)
34+
}
35+
```
1436

15-
[coveralls-badge]: https://img.shields.io/coveralls/user/repo/master.png?style=flat-square
16-
[coveralls]: https://coveralls.io/github/user/repo

demo/src/index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
import React, {Component} from 'react'
1+
import React from 'react'
22
import {render} from 'react-dom'
33

4-
import Example from '../../src'
4+
import useFetch from '../../src'
55

6-
export default class Demo extends Component {
7-
render() {
8-
return <div>
9-
<h1>useFetch Demo</h1>
10-
<Example/>
11-
</div>
12-
}
6+
const Example = () => {
7+
const { isLoading, error, data} = useFetch('https://get.geojs.io/v1/ip/country.json?ip=8.8.8.8')
8+
9+
if (isLoading) {
10+
return <p>Loading.....</p>
11+
}
12+
13+
if (error) {
14+
return <p>{JSON.stringify(error)}</p>
15+
}
16+
17+
return (
18+
<div>
19+
<p>
20+
{JSON.stringify(data)}
21+
</p>
22+
</div>
23+
)
1324
}
1425

15-
render(<Demo/>, document.querySelector('#demo'))
26+
render(<Example/>, document.querySelector('#demo'))

0 commit comments

Comments
 (0)