Skip to content

Home

Uncontrolled input field

Renders an uncontrolled <input> element that uses a callback function to inform its parent about value updates.

const UncontrolledInput = ({ defaultValue, onValueChange, ...rest }) => {
  return (
    <input
      defaultValue={defaultValue}
      onChange={({ target: { value } }) => onValueChange(value)}
      {...rest}
    />
  );
};

ReactDOM.createRoot(document.getElementById('root')).render(
  <UncontrolledInput
    type="text"
    placeholder="Insert some text here..."
    onValueChange={console.log}
  />
);

More like this

Start typing a keyphrase to see matching snippets.