react-use-counter

React hook for counter state.

Usage no npm install needed!

<script type="module">
  import reactUseCounter from 'https://cdn.skypack.dev/react-use-counter';
</script>

README

react-use-counter

CircleCI Coverage Status

React hook for counter state.

Installation

Using npm:

$ npm install --save react-use-counter

Using yarn:

$ yarn add react-use-counter

Since this module uses React Hooks you'll need to install at least the 16.8.0 version of react and react-dom:

$ yarn add react@^16.8.0 react-dom@^16.8.0

Usage

import React from 'react';
import useCounter from 'react-use-counter';

function Example() {
  const [count, increment] = useCounter(0);

  return (
    <div>
      You clicked {{count}} times.

      <button onClick={increment}>Click me!</button>
    </div>
  );
}

API

useCounter(initialCount?: number): [number, () => void]

Receives an optional initial count and returns a tuple with the current count and a function to increment the counter.

Contributing

Please feel free to submit any issues or pull requests.

License

MIT