Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-incrementor

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-incrementor

Component for building flexible incrementor components

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

✨react-incrementor🎄

Build Status Code Coverage downloads PRs Welcome MIT License

Component to build simple, flexible, and accessible incrementor components

The problem

You want an incrementor component that's simple and gives you complete control over rendering and state.

This solution

This follows the controlled prop pattern and render prop pattern to expose an API that renders nothing and simply encapsulates the logic of a incrementor component.

Table of Contents

Installation

This module is distributed via npm, which is bundled with node, and should be installed as one of your project's dependencies:

npm install --save react-incrementor

This package also depends on react and prop-types. Please make sure you have those installed as well.

Usage

import React from 'react';
import { render } from 'react-dom';

import Incrementor from 'react-incrementor';

function BasicIncrementor() {
  return (
    <Incrementor
      render={({ value, getDecrementerProps, getIncrementerProps }) => {
        return (
          <div>
            <div>
              <span>{value}</span>
            </div>

            <div>
              <button {...getDecrementerProps()}>Minus 1</button>
              <button {...getIncrementerProps()}>Plus 1</button>
            </div>
          </div>
        );
      }}
    />
  );
}

render(<BasicIncrementor />, document.getElementById('root'));

react-incrementor is the only component. It doesn't render anything itself, it just calls the render function and renders that.

Props:

defaultValue

number | defaults to 0

The default value state.

value

number | control prop

react-incrementor manages its own state internally and calls your onChange handler whenever the value state changes. Pass the value state as a prop and that state becomes controlled. It is your responsibility to keep the value updated by re-rendering the component.

onChange

function(value: number) | optional

Called at interaction with the incrementor

render

function({}) | required

This is called with an object that exposes the public API of this component.

The function is passed as the render prop: <Incrementor render={(value) => {/* awesome code */}} />

propertycategorytypedescription
valuestatebooleanThe current value state of the incrementor
getIncrementerPropsprop getterfunction(props: object)returns the props you should apply to the incrementer button element you render. Includes aria- attributes
getDecrementerPropsprop getterfunction(props: object)returns the props you should apply to the decrementer button element you render. Includes aria- attributes

Inspiration

This was build as a learning exercise for the controlled prop pattern and render prop pattern. It is heavily influenced by Kent C. Dodds's react-toggled component

LICENSE

MIT

Keywords

FAQs

Package last updated on 28 Jan 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