Socket
Socket
Sign inDemoInstall

react-loading-switch

Package Overview
Dependencies
3
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-loading-switch

React component API for easily composing the render logic surrounding react-apollo data fetching, loading, and error handling


Version published
Maintainers
1
Created

Readme

Source

react-loading-switch 🐶

React component API for easily composing the render logic surrounding react-apollo data fetching, loading, and error handling.

Compatible with React, React Native, React Web, React anything!

The Problem:

In our experience, re-writing identical or similar logic in every component is bad.

  • ❌ Error prone.
  • ❌ Multiple programming styles result in very different-looking code.
  • ❌ Difficult to digest at a glance.
  • ❌ Problem grows as codebase grows.
This sucks 👎
render = () => {
  const { loading, error, puppy } = this.props.data

  if (error) {
    return <RenderError error={error} />
  }

  if (!puppy) {
    if (loading) {
      return <RenderLoading />
    }

    return <RenderError error={new Error('Could not find puppy!')} />
  }

  return (
    <View>{ `Finally the puppy is here! ${puppy.id}` }</View>
  )
}

Instead, compose your rendering with react-loading-switch!

  • ✅ Consistent JSX component API.
  • ✅ Easy to digest at a glance.
  • ✅ Extensible & Functional
  • ✅ Centralized default configuration across multiple components if desired.
  • ✅ It's just a react component. Wrap it with default props if you want to share functionality between components.
Meet <LoadingSwitch /> 👍
import LoadingSwitch from 'react-loading-switch'

render() {
  const { loading, error, puppy } = this.props.data

  return (
    <LoadingSwitch
      error={error}
      errorWhenMissing={() => new Error('Missing puppy data!')}
      loading={loading}
      renderError={(error) => <DataError error={error} />}
      renderLoading={() => <Loading />}
      require={puppy}
    >
      { () => (
        <View>{ `The puppy data is here! ${puppy.id}` }</View>
      ) }
    </LoadingSwitch>
  )
}
DRY it up by wrapping with some default props 🎉
import MyLoadingSwitch from '../MyLoadingSwitch'

render() {
  const { loading, error, puppy } = this.props.data

  return (
    <MyLoadingSwitch
      error={error}
      loading={loading}
      require={puppy}
    >
      { () => (
        <View>{ `The puppy data is here! ${puppy.id}` }</View>
      ) }
    </MyLoadingSwitch>
  )
}
With React-Apollo's shiny new <Query /> components :godmode:
import LoadingSwitch from 'react-loading-switch'
import { Query } from 'react-apollo'

const GET_PUPPY = gql`
  query puppy($puppyId: ID!) {
    puppy(id: $puppyId) {
      id
      name
      birthday
    }
  }
`;

const PuppyBirthday = ({ puppyId }) => (
  <Query query={GET_PUPPY} variables={{ puppyId }}>
    {({ loading, error, data: { puppy } }) => (
      <LoadingSwitch
        error={error}
        errorWhenMissing={() => new Error('Missing puppy birthday data!')}
        loading={loading}
        renderError={(error) => <PuppyBirthdayError error={error} />}
        renderLoading={() => <PuppyBirthdayLoading />}
        require={puppy}
      >
        { () => (
          <View>{ `${name}'s birthday is ${puppy.birthday}!` }</View>
        ) }
      </LoadingSwitch>
    )}
  </Query>
)
Require multiple things! It uses JavaScript's truthy/falsey checking.

*Remember falsey in JavaScript: false || null || undefined || 0 || '' || NaN * :neckbeard:

render() {
  const { loading, error, someData, moreData } = this.props.data

  return (
    <MyLoadingSwitch
      error={error}
      loading={loading}
      require={someData && moreData && moreData.hasTheRequiredThing}
    >
      { () => (
        <View>{ moreData.hasTheRequiredThing }</View>
      ) }
    </MyLoadingSwitch>
  )
}

Getting Started

npm i --save react-loading-switch

API

See the test/ directory in this repo for detailed snapshot tests that cover the whole API.

Keywords

FAQs

Last updated on 24 Apr 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc