Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

use-merge-value

chenshuai2144401.3kMIT1.2.0TypeScript support: included

Easier to write a controlled component

readme

use-merge-value

Easier to write a controlled component

此 hooks 可以轻松的实现一个受控组件。使用方式类似于 useState ,支持通过第二个参数传入 { value, onChange} 来覆盖掉默认的 value 与 setValue。

Usage

yarn add use-merge-value
import React, { useState } from 'react';
import useMergeState from 'use-merge-value';

const ControlledInput: React.FC<{ value: string; onChange: (value: string) => void }> = props => {
  const [value, setValue] = useMergeState('', props);
  return (
    <div>
      <input value={value} onChange={e => setValue(e.target.value)}></input>{' '}
    </div>
  );
};

LICENSE

MIT