简体   繁体   English

在 React 中将商品添加到购物车时无法输入输入字段

[英]Unable to type in an input field when adding item to cart in React

The logic I'm try to apply here may be totally wrong.我在这里尝试应用的逻辑可能完全错误。 This is the first time I'm attempting to do something like this (trying to use an input field to get value of the number of items to be added to cart).这是我第一次尝试做这样的事情(尝试使用输入字段来获取要添加到购物车的项目数量的值)。 I'm not able to type in the input field.我无法在输入字段中输入。

    const [qty, setQty] = useState(1)
    const submitHandler = (e) => {
        e.preventDefault()
        if(e.target.value <=product.countInStock && e.target.value > 0){
                setQty(e.target.value)
                history.push(`/cart/${match.params.id}?qty=${qty}`)
        }else{
            alert("Insifficient Quantity in Stock")
            }
    }
========================================
<Row>
    <Col sm={4}>Qty</Col>
    <Col sm={8}>
    <Form onSubmit={submitHandler}>
        <Form.Control
            type="number"
            min="0"
            max={product.countInStock} 
            value={qty}
            onchange={checkInput}                                                         
            />
        <Button
               onClick={addToCartHandler}
                size="lg"
                disabled={product.countInStock === 0}
                type='button'>
                Add to Cart
             </Button>

         </Form>
    </Col>
</Row>

How do I fix it?我如何解决它? It seems to be a popular issue that people have asked question on online but still can't find solution to mine这似乎是人们在网上提出问题但仍然找不到解决方案的热门问题

It seems that you have a problem with your event handlers.您的事件处理程序似乎有问题。 Since you pass the value={qty} to the input field, you create a controlled component .由于您将value={qty}传递给输入字段,因此您创建了一个受控组件 However upon updating the input you don't update the parent's state, so in the end the input's value doesn't change.但是,在更新输入时,您不会更新父级的 state,因此最终输入的值不会改变。

Try something like this on your input:在你的输入上尝试这样的事情:

<Form.Control
    type="number"
    min={0}
    max={product.countInStock}
    value={qty}
    onchange={(e) => setQty(e)}/> 

(I assume the onChange returns the new value as number. If it doesn't, you'll need to transform the returned data accordingly before checking it in the submit event handler.) (我假设 onChange 将新值作为数字返回。如果没有,您需要相应地转换返回的数据,然后在提交事件处理程序中检查它。)

Also update the submitHandler to be: submitHandler更新为:

const submitHandler = (e) => {
    e.preventDefault()
    if (qty <= product.countInStock && qty > 0) {
        history.push(`/cart/${match.params.id}?qty=${qty}`)
    } else {
        alert('Insufficient Quantity in Stock')
    }
}

Daniel's answer is correct in that a controlled input field won't change its value unless you change the state variable that controls it.丹尼尔的回答是正确的,除非您更改控制它的 state 变量,否则受控输入字段不会更改其值。 So really what you need is to put setQty(e.target.value) inside your checkInput function.所以你真正需要的是将setQty(e.target.value)放在你的checkInput function 中。 That should do the trick.这应该够了吧。

You could also use useRef to control your form.您还可以使用useRef来控制您的表单。 That way there won't be a need to rerender your elements every time there is a change in the input field.这样,每次输入字段发生变化时都不需要重新渲染元素。 Let me know if you're interested to see how that would look in your case.如果您有兴趣了解您的情况如何,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM