I'm learning React and I created a really simple "shopping list app". Everything is working now but I'm getting this error: "Warning: A component is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component".
Here it is my code:
function InputArea(props) {
const [inputText, setInputText] = useState({
inputText: {text: ""}
});
function handleChange(event){
setInputText(event.target.value);
}
function handleClick(event) {
props.onSubmit(inputText);
setInputText({text: ""});
event.preventDefault();
}
return(
<div className="input-group w-50">
<input
type="text"
className="form-control"
onChange={handleChange}
ariadescribedby="button-addon"
value={inputText.text}
placeholder="Insert Item">
</input>
<div className="input-group-append">
<Button
id="button-addon"
color="dark"
style={{marginBottom: "2rem"}}
onClick={handleClick}>Add Item
</Button>
</div>
</div>
)
}
The problem appears when I want to reset my input, in order to see the placeholder instead of the name of the last item added.