B
B
Bogdnan1232020-10-04 19:05:04
JavaScript
Bogdnan123, 2020-10-04 19:05:04

How to set default value in 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 have a value from the server, and when I change it, as you can see in the code, when the focus is removed onBlur, the data is sent to the server

But when I delete the values ​​from input and 1 character remains, then the value that was returned by default is returned, respectively, I cannot clear the input

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
i1yas, 2020-10-04
@i1yas

const [nameValue, setNameValue] = useState(name || "");

<TextField
  ...
  value={nameValue}
  ...
/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question