New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bananahooks

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bananahooks

"Collection" of custom React Hooks.

  • 0.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Bananahooks

"Collection" of custom React Hooks.

Requirements:

  • React 16.8.0 or higher

Install

npm install bananahooks

or

yarn add bananahooks

Develop

yarn install

yarn test

yarn build

Releases

yarn build
npm version [patch|minor|major]
npm publish

usePromise

Custom React Hook that properly awaits a promise to resolve.
It automatically 'unsubscribes' if the component is unmounted.

Here's an example on CodeSandbox:
Edit pkk3xjn00m

Usage

Import the function:

import { usePromise } from 'bananahooks';

Basic usage:

const App = ({ client }) => {

  const [bananas] = usePromise(client.fetchBananas, [])

  return (
    <ul>
    {bananas.map(banana => <li>{banana}<li>)}
    </ul>
  )
}

The first render will use the empty array passed as a default value.
The next render will -probably- have some bananas in the bananas array.

Note that there is no need to useState to keep track of bananas.

Advanced usage:

Handling errors and loading state.

const App = ({ client }) => {

  const [bananas, error, pending] = usePromise(client.fetchBananas, [])

  return (
    <ul>
    {pending && <li>Loading...</li>}
    {!pending && error && <li>Error! {error.toString()}</li>)}
    {!pending && !error && bananas.map(banana => <li>{banana}<li>)}
    </ul>
  )
}

On the first render pending is true and the "loading" message is rendered.
The next time the promise will have resolved or rejected. error will be 'truthy' if the promise was rejected, and error message can be displayed. In most cases the promise will have resolved and the bananas array is displayed.


This project was bootstrapped with TSDX:

TSDX Bootstrap

This project was bootstrapped with TSDX.

Local Development

Below is a list of commands you will probably find useful.

npm start or yarn start

Runs the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab.

Your library will be rebuilt if you make edits.

npm run build or yarn build

Bundles the package to the dist folder. The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).

npm test or yarn test

Runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.

Keywords

FAQs

Package last updated on 14 May 2019

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc