useInput
Aboutβ
Input hook for React.
Installationβ
npm install --save rooks
Importing the hookβ
import { useInput } from 'rooks';
Usageβ
Base
function Demo() {
const myInput = useInput('hello');
return (
<div>
<input {...myInput} />
<p>
Value is <b>{myInput.value}</b>
</p>
</div>
);
}
render(<Demo />);
With optional validator
function Demo() {
const myInput = useInput('hello', {
validate: (newValue) => newValue.length < 15,
});
return (
<div>
<p> Max length 15 </p>
<input {...myInput} />
<p>
Value is <b>{myInput.value}</b>
</p>
</div>
);
}
render(<Demo />);
Argumentsβ
Argument | Type | Description | Default value |
---|---|---|---|
initialValue | string | Initial value of the string | "" |
opts | object | Options | {} |
Optionsβ
Option key | Type | Description | Default value |
---|---|---|---|
validate | function | Validator function which receives changed valued before update as well as current value and should return true or false | undefined |
Return valueβ
Return value | Type | Description |
---|---|---|
{value, onChange} | Object | Object containing {value : "String", onChange: "function that accepts an event and updates the value of the string"} |
Codesandbox Examplesβ
Basic Usageβ
Join Bhargav's discord serverβ
You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.