How to set a default value in the input and change it?
-
const [nameValue, setNameValue] = useState(""); <TextField id="outlined-read-only-input" label="Display Name" variant="outlined" value={nameValue !== '' ? nameValue : name || '' } onChange={(e) => setNameValue(e.target.value)} onBlur={() => setName({ variables: { name: nameValue } })} />
name - the value that comes from the server
nameValue - the new value that I need to set
By default, the input should contain a value from the server, and when I change it, as you can see in the code, when the focus is removedonBlur
, the data is sent to the server.
But when I remove the values from the input and 1 character remains, then the value that was by default is returned, so I cannot clear the inputJavaScript Andrew Montgomery, Aug 21, 2020 -
const [nameValue, setNameValue] = useState(name || "");
<TextField
...
value={nameValue}
...
/>Anonymous
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!