3.1.3 • Published 4 years ago

react-captain v3.1.3

Weekly downloads
14
License
MIT
Repository
github
Last release
4 years ago

: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.

3.1.3

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.8

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.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.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.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

0.0.21-alpha

5 years ago

0.0.20-alpha

5 years ago

0.0.19-alpha

5 years ago

0.0.18-alpha

5 years ago

0.0.17-alpha

5 years ago

0.0.16-alpha

5 years ago

0.0.15-alpha

5 years ago

0.0.14-alpha

5 years ago

0.0.13-alpha

5 years ago

0.0.12-alpha

5 years ago

0.0.11-alpha

5 years ago

0.0.10-alpha

5 years ago

0.0.9-alpha

5 years ago

0.0.8-alpha

5 years ago

0.0.6-alpha

5 years ago

0.0.5-alpha

5 years ago

0.0.4-alpha

5 years ago

0.0.3-alpha

5 years ago

0.0.2-alpha

5 years ago

0.0.1-alpha

5 years ago