@rcp/use.uncontrolled
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

@rcp/use.uncontrolled

NPM version NPM Downloads

Make props.value piped to state, and exposes onChange, make react component is uncontrolled & controlled

Installation

npm install @rcp/use.uncontrolled
# or use yarn
yarn add @rcp/use.uncontrolled

Usage

import useUncontrolled from '@rcp/use.uncontrolled'

API

useUncontrolled

Parameters

  • value {T} - Piped value
  • defaultValue {T} - Initialize value firstly
  • onChange {(value: T) => void} - Bind onChange handler when value updating
  • useEffect {typeof React.useLayoutEffect}
  • eq {(a: T, b: T) => boolean}

Examples

function Input({ value, onChangeValue, defaultValue }) {
  const [valueState, setValue] = useUncontrolled({ value, onChange: onChangeValue, defaultValue })

  return <input type="text" value={valueState} onChange={(evt) => setValue(evt.target.value)} />
}

Returns Array [T, ((value: T) => T | T) => void]

Related

Authors

This library is written and maintained by imcuttle, moyuyc95@gmail.com.

License

MIT

Package Sidebar

Install

npm i @rcp/use.uncontrolled

Weekly Downloads

20

Version

3.0.0

License

MIT

Unpacked Size

21.1 kB

Total Files

14

Last publish

Collaborators

  • moyuyc