2

I work on a project in React, and I have an input in a table for the quantity of a product I want to buy, and I can't change the value on the web page.

If I delete the value property, I can modify the value, and I think I typed something wrong. Can somebody help me with how to have an initial value equal to 1 and still be able to update it?

<input
  type="text"
  id={"cantitate_" + index}
  value={1}
  onChange={(e) => changeQuantity(e.target.value, index)}
/>

changeQuantity is a function where I try to calculate something.

1 Answer 1

3

You should be using defaultValue instead of value, since I assume from the comment that you are not implementing a controlled form:

<input
  type="text"
  id={"cantitate_" + index}
  defaultValue={1}
  onChange={(e) => changeQuantity(e.target.value, index)}
/>
Sign up to request clarification or add additional context in comments.

2 Comments

i don't have any variable quantity saved. All I want is that the value to be 1 for start and to can change the value
In this case use defaultValue instead, I just edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.