mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Read Only Input Elements as a react til
This commit is contained in:
31
react/read-only-input-elements.md
Normal file
31
react/read-only-input-elements.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Read Only Input Elements
|
||||
|
||||
Here is an input element with a `value` and no `onChange` handler.
|
||||
|
||||
```javascript
|
||||
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.
|
||||
|
||||
```javascript
|
||||
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
|
||||
Reference in New Issue
Block a user