Reputation: 301
I have added a simple TextField from Material-UI into my landing page but cannot type in the React input field. I'm not sure what is causing the issue and have double-checked the following issues:
onChange
(not onchange
)this.state
vs state
; the former causes a TypeError: Cannot read properties of undefined (reading 'state')
.type="text"
vs. type="number"
; both options don't allow for typing in the text fieldBelow is my code for the TextField
, declaring state
, and handling the change in input field.
import TextField from '@material-ui/core/TextField';
...
const [state, setState] = useState({
ImageNumber: '',
});
const handleChange = (event) => {
setState({
...state,
[event.target.number]: event.target.value,
});
};
...
<TextField label="" number="ImageNumber" type="text" value={state.ImageNumber} variant="outlined" onChange={handleChange} />
Upvotes: 1
Views: 3619
Reputation: 1827
import TextField from '@material-ui/core/TextField';
...
const [state, setState] = useState({
ImageNumber: '',
});
const handleChange = (event) => {
setState({
...state,
[event.target.name]: event.target.value,
});
};
...
<TextField label="" name="ImageNumber" type="text" value={state.ImageNumber} variant="outlined" onChange={handleChange} />
Instead of a non-existent <input />
element props number
, you can use the name
attribute, which shouldn't show undefined
.
Upvotes: 1
Reputation: 485
handleChange
method import TextField from '@material-ui/core/TextField';
...
const [state, setState] = useState("");
const handleChange = (event) => {
setState(event.target.value);
};
...
<TextField label="" number="ImageNumber" type="text" value={state} variant="outlined" onChange={(e) => handleChange(e)} />
Upvotes: 3