Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 776 Bytes

read-only-input-elements.md

File metadata and controls

31 lines (24 loc) · 776 Bytes

Read Only Input Elements

Here is an input element with a value and no onChange handler.

const MyInput = ({ value }) => {
  return (
    <input value={value} />
  );
};

React will raise a warning regarding the input element because it has a value without an onChange handler leaving React to wonder if it is intended to be a controlled or uncontrolled component.

If our intention is to have the value set but not allow the user to directly change it, we just need to let React know that.

const MyInput = ({ value }) => {
  return (
    <input readOnly value={value} />
  );
};

The readOnly prop means we don't intend for the input to be modified by user input. The React warning will now go away.

h/t Dillon Hafer