admin管理员组文章数量:1333693
This is my code:
const [defaultBuy, setDefaultBuy] = useState({
data: '',
price: 0
})
const changeBuyPrice = (e: any) => {
let value = e.target.value
if (value > 1000) {
value = 9000
}
console.log(value)
setDefaultBuy({...defaultBuy, price: value})
}
And this is render:
<input defaultValue={defaultBuy.price} name="" type="text" onChange={(e) => changeBuyPrice(e)} />
If I put number larger than 1000
in input, console.log(value)
show me 9000
.
But <input>
tag not show 9000
.
I want to change <input>
value also
This is my code:
const [defaultBuy, setDefaultBuy] = useState({
data: '',
price: 0
})
const changeBuyPrice = (e: any) => {
let value = e.target.value
if (value > 1000) {
value = 9000
}
console.log(value)
setDefaultBuy({...defaultBuy, price: value})
}
And this is render:
<input defaultValue={defaultBuy.price} name="" type="text" onChange={(e) => changeBuyPrice(e)} />
If I put number larger than 1000
in input, console.log(value)
show me 9000
.
But <input>
tag not show 9000
.
I want to change <input>
value also
-
1
try
value={defaultBuy.price}
– Amaarockz Commented Feb 21, 2022 at 7:41 -
1
onChange={(e) => changeBuyPrice(e)}
— creating a new function like that is redundant. Just pass the function you have:onChange={changeBuyPrice}
– Quentin Commented Feb 21, 2022 at 7:42
2 Answers
Reset to default 3If you want the input to reflect the value in the state then you need to make it a controlled ponent.
Assign the value in the state to the value
prop instead of the defaultValue
prop.
At the end, I did e.target.value = value to update the value of the input field
const changeBuyPrice = (e: any) => {
let value = e.target.value
if (value > 1000) {
value = 9000
}
console.log(value)
setDefaultBuy({...defaultBuy, price: value})
e.target.value = value // add this here to update your input field
}
本文标签: javascriptNextjs onChange input not change value on selfStack Overflow
版权声明:本文标题:javascript - Nextjs: onChange input not change value on self - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742346644a2457656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论