npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-captain

v3.1.3

Published

A collection of strongly typed React hooks and contexts.

Downloads

53

Readme

:anchor: React Captain Build Status codecov npm

A collection of strongly typed React hooks and contexts.

Live demo at https://react-captain.soywod.me.

Table of contents

Installation

yarn add react-captain
# or
npm install react-captain

Examples

Live demo at https://react-captain.soywod.me.

Click outside

Capture click event outside of the given HTMLElement.

import {useClickOutside} from "react-captain"

const Component: FC = () => {
  const ref = useRef<HTMLDivElement | null>(null)
  useClickOutside(ref, () => console.log("Clicked outside!"))

  return (
    <div ref={ref}>
      Click outside
    </div>
  )
}

Debounce

Add debounce to a handler.

import {useDebounce} from "react-captain"

function Component() {
  const handler = useDebounce(() => console.log("Hello!"), 1000)

  return (
    <>
      <button onClick={handler}>
        Say hello with delay
      </button>
      <button onClick={handler.abort}>
        Abort
      </button>
      <button onClick={handler.terminate}>
        Terminate
      </button>
    </>
  )
}

Timeout

A wrapper around setTimeout.

import {useTimeout} from "react-captain"

function Component() {
  const handler = useTimeout(() => console.log("Hello!"), 1000)

  return (
    <>
      <button onClick={handler}>
        Say hello with delay
      </button>
      <button onClick={handler.abort}>
        Abort
      </button>
      <button onClick={handler.terminate}>
        Terminate
      </button>
    </>
  )
}

Interval

A wrapper around setInterval, using toggle.

import {useInterval} from "react-captain"

function Component() {
  const [isOn, toggle] = useInterval(() => console.log("Hello!"))

  return (
    <button onClick={handler}>
      {isOn ? "Stop" : "Say hello"}
    </button>
  )
}

Stored state

A persistant useState, based on React's useState and localForage. Drivers supported: localStorage, WebSQL and IndexedDB.

import {useStoredState} from "react-captain"

function Component() {
  const [value, setValue] = useStoredState("storage-key", "Default value")

  return (
    <button onClick={() => setValue("Value changed!")}>
      {String(value)}
    </button>
  )
}

Toggle

A useState for booleans.

import {useToggle} from "react-captain"

const Component: FC = () => {
  const [isOn, toggle] = useToggle(false)

  return (
    <div>
      <button onClick={toggle}>
        Switch status: {isOn ? "ON" : "OFF"}
      </button>
      <button onClick={() => toggle(false)}>
        Reset toggle
      </button>
    </div>
  )
}

Subject

A wrapper around rxjs.Subject.

import {useSubject} from "react-captain"
import {Subject} from "rxjs"

const subject$ = new Subject<number>()

const Component: FC = () => {
  const [counter, setCounter] = useState(0)
  useSubject(subject$, setCounter)
  return <button onClick={() => subject$(counter + 1)}>{counter}</button>
}

Behavior subject

A wrapper around rxjs.BehaviorSubject.

import {useBehaviorSubject} from "react-captain"
import {BehaviorSubject} from "rxjs"

const subject$ = new BehaviorSubject(0)

const Component: FC = () => {
  const [counter, setCounter] = useBehaviorSubject(subject$, counter => {
    console.log("New counter received:", counter)
  })

  return <button onClick={() => setCounter(counter + 1)}>{counter}</button>
}

Development

git clone https://github.com/soywod/react-captain.git
cd react-captain
yarn install

To start the development server:

yarn start

To build the lib:

yarn build

To build the demo:

yarn build:demo

Tests

Unit tests

Unit tests are handled by Jest (.test files) and React Testing Library (.spec files).

yarn test:unit

End-to-end tests

End-to-end tests are handled by Cypress (.e2e files).

yarn start
yarn test:e2e

Changelog

See CHANGELOG.md.

License

MIT - Copyright © 2019-2020 soywod.