rc-value

0.0.3 • Public • Published

RC Value · Build Status Coverage Status

Make React component switch between Controlled Component and Uncontrolled Component easily.

Installation

yarn add rc-value or npm install rc-value --save

Live Demos

You can find some demos at storybook.

Usage

Value

import { Value } from 'rc-value'
 
const Switch = props => (
  <Value {...props}>
    {({ value, onChange }) => (
      <button
        onClick={() => { onChange(prev => !prev) }}
      >
        {String(value)}
      </button>
    )}
  </Value>
)
 
<Switch defaultValue={false} onChange={console.log} /> // This becomes an Uncontrolled Switch
<Switch value={false} onChange={console.log}/> // This becomes a Controlled Switch

useValue

import { useValue } from 'rc-value'
 
const Switch = (props) => {
  const { value, onChange } = useValue(props)
  return <button onClick={() => onChange(prev => !prev)}>{String(value)}</button>
}
 
<Switch defaultValue={false} onChange={console.log} /> // This becomes an Uncontrolled Switch
<Switch value={false} onChange={console.log}/> // This becomes a Controlled Switch

withValue

import { withValue } from 'rc-value'
 
const Switch = withValue(
  ({ value, onChange }) => (
    <button onClick={() => onChange(prev => !prev)}>{String(value)}</button>
  ),
)
 
<Switch defaultValue={false} onChange={console.log} /> // This becomes an Uncontrolled Switch
<Switch value={false} onChange={console.log}/> // This becomes a Controlled Switch

Readme

Keywords

none

Package Sidebar

Install

npm i rc-value

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

566 kB

Total Files

22

Last publish

Collaborators

  • beizhedenglong