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

react-on-off-state

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-on-off-state

React component to manage on/off states

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source
react-on-off-state

React component to manage on/off states

Travis Codecov npm version code style: prettier

Installation

npm install react-on-off-state
# or
yarn add react-on-off-state

Usage

import React from "react";
import ReactDOM from "react-dom";
import OnOffState from "react-on-off-state";

ReactDOM.render(
  <OnOffState>
    {({ on, toggle }) => (
      <>
        <h1>{on ? "Red" : "Blue"}</h1>
        <button onClick={toggle}>Change pill</button>
      </>
    )}
  </OnOffState>,
  document.getElementById("root")
);

Props

defaultOn

boolean | optional, defaults to false

The initial on state.

on

boolean | optional, control prop

By default, react-on-off-state manages its own state. You can use the on prop if you want to control the state by yourself.

onChange

(on: boolean) => any | optional

Called whenever the state changes.

  • on: boolean – The new state

children

(options: RenderOptions) => React.Node | required

The children prop is treated as a render prop. This is where you render whatever you want based on the state of react-on-off-state.

  • options: RenderOptions – An object with the following properties:
PropertyDescription
on: booleantrue if the state is on, false otherwise
off: booleanconvenience prop if you need !on
setOn: () => voidSets the state to on
setOff: () => voidSets the state to off
toggle: () => voidToggles the state (i.e. when it's on, will set to off and vice versa)

Inspiration

The design and API mostly comes from react-toggled. react-on-off-state is a more generic version of it that only manages state and nothing more. You can think of it as a pre-build state machine. It's useful whenever you want to render not just a toggle component but anything that has two states.

LICENSE

MIT

Keywords

FAQs

Package last updated on 15 Apr 2018

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