Code Monkey home page Code Monkey logo

with-fetch's Introduction

with-fetch

HOC for simple data fetching designed for fetch api. If you pass it a request function that is not following the fetch api, the raw response will be returned to the final component in the data prop.

Install

$ yarn add with-fetch
$ npm install with-fetch

How it works

The component exports two HOC's

  • withFetch
  • displayWhileLoading

withFetch

This HOC is where the fetching is done. The HOC will fetch, parse and then pass the data to the final component.

Basic Usage

import {withFetch} from 'with-fetch'
import {compose} from 'recompose'
import fetch from 'isomorphic-fetch'

export const MyComopnent = compose(
  withFetch(props => fetch(`http://myResource.com/${props.someResourceId}`))
)(({data, loading, error}) => {
  if (error) {
    return <MyErrorComponent error={error} />
  }

  if (loading) return <MySpinner />

  return <div>{data}</div>
})

displayWhileLoading

This HOC displays a spinner, it comes with a somewhat ugly default spinner but accepts a custom spinner. The HOC is designed to take up 100% space of the wrapper component.

Basic Usage (Combined with withFetch)

import {withFetch, displayWhileLoading} from 'with-fetch'
import {compose} from 'recompose'
import fetch from 'isomorphic-fetch'

const MyCustomSpinner = () => <div>Spinning into places</div>

const enhance = compose(
  withFetch(props => fetch(`http://myResource.com/${props.someResourceId}`)),

  // Your spinner will be displayed until the data is returned

  // ####################################################################
  // If you dont pass any argument, the HOC will render a default spinner
  // ####################################################################
  displayWhileLoading(MyCustomSpinner)
)

const MyComponent = enhance(({data, loading, error}) => {
  // If you dont want this inside your render, move this functionality into a HOC aswell!
  if (error) return <MyErrorComponent error={error} />
  return <div>{data}</div>
})

with-fetch's People

Contributors

shoulderkarlsson avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.