0.4.0 • Published 5 years ago

fetch-react v0.4.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

fetch-react · GitHub license npm version CircleCI Status PRs Welcome

fetch-react is a very small (<4kb) React Higher-Order Component that wraps the Fetch API in a declarative way.

<Fetch
  url={url}
  opts={opts}
  onData={onData}
  onLoading={onLoading}
  onError={onError}
  readFn={readFn}
  fetchFn={fetchFn}
/>

Props

  • url - the request url - a string, URL or Request object
  • onData - a function that renders the response data
  • onLoading - (optional) a function that renders the loading state
  • onError - (optional) a function that renders the error state
  • opts - (optional) request options are passed directly to fetch()
  • readFn - (optional) function to read the Response body (defaults to resp => resp.json())
  • fetchFn - (optional) fetch() implementation to use (defaults to window.fetch)

Installation

npm i fetch-react

For older browsers without native fetch support you need a polyfill.

For Node.js usage you need node-fetch.

Usage

import React from 'react'
import Fetch from 'fetch-react'

const GitHubUser = ({ name }) =>
  <Fetch
    url={'https://api.github.com/search/users?q=' + name}
    onData={data => <img src={data.items[0].avatar_url}/>}
    onLoading={() => 'Loading...'}
    onError={error => 'An error occured!'}
  />

// and use like

<GitHubUser name="alexanderdzhoganov"/>

Fetch options

You can pass options to the fetch call using the opts prop. Valid options are described here.

Default options and base URL

Wrap the component to set default options or a base URL.

const fetchOpts = { credentials: 'same-origin' }

const ApiFetch = props => <Fetch
  url={new URL('https://api.example.com/v1/', props.url)}
  opts={fetchOpts}
  { ...props }
/>

// then use like

<ApiFetch
  url="/user/11"
  onData={user => <User user={user}/>}
/>

Handling errors in the response

You can throw from onData which will render onError with the thrown error as first argument.

Alternative withFetch API

import React from 'react'
import { withFetch } from 'fetch-react'

class MyComponent extends React.Component {
  render() {
    const { fetchResponse, fetchError } = this.props

    if (!fetchResponse && !fetchError) {
      return <div>Loading!</div>
    }

    if (fetchError) {
      return <div>Error!</div>
    }

    return <img src={fetchResponse.avatar_url}/>
  }
}

const url = 'https://api.github.com/search/users?q=alexanderdzhoganov'
const fetchOpts = { credentials: 'same-origin' }

const WrappedComponent = withFetch(url, fetchOpts)(MyComponent)
0.4.0

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.18

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago