Skip to main content
Version: v3.6.0

@rooks/use-input

Input hook for React.


Build Status

Installation

npm install --save @rooks/use-input

Importing the hook

import useInput from "@rooks/use-input"

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

ArgumentTypeDescriptionDefault value
initialValuestringInitial value of the string""
optsobjectOptions{}

Options

Option keyTypeDescriptionDefault value
validatefunctionValidator function which receives changed valued before update as well as current value and should return true or falseundefined

Return value

Return valueTypeDescription
{value, onChange}ObjectObject containing {value : "String", onChange: "function that accepts an event and updates the value of the string"}