简体   繁体   English

如何在反应中为特定元素触发 onChange 事件

[英]How to fire onChange event for specific element in react

Hi guys am new to react and after reading the doc.嗨,伙计们是新来的,在阅读了文档后做出了反应。 i jump straigth to developmentwith react, I have a set of elements with the fire the same onChange event now the problem i have is all the element are control rather than the targeted one我直接跳到开发与反应,我有一组元素与火相同的 onChange 事件现在我遇到的问题是所有元素都是控制而不是目标元素

 function handleOnChange(e){
    let value = e.target.value
    if(isNaN(value)){
      value = 0
    }
    else if(value === "" || value <= 0){
      value = 0
    }
    setInfo(preInfo => {
      return{
        ...preInfo,
        quantity:value,
        
      }
    })
  }



 <div className="input-group buynum_select">
       <span className="input-group-btn">
       <button className="btn minus" 
         onClick={e => handlerUpdateQuantity(-1)}>-</button>
                            </span>
           <input maxLength="7" className="form-control" value={info.quantity} 
            onChange={e => handleOnChangeQuantity(e)}
            onKeyUp={e => handleKeyUpQuantity(e)} type="text"  
                   />
            <span className="input-group-btn">
             <button className="btn plus"
            onClick={e => handlerUpdateQuantity(1)}>+</button>
             </span>
        </div>

 <div className="input-group buynum_select">
       <span className="input-group-btn">
       <button className="btn minus" 
         onClick={e => handlerUpdateQuantity(-1)}>-</button>
                            </span>
           <input maxLength="7" className="form-control" value={info.quantity} 
            onChange={e => handleOnChangeQuantity(e)}
            onKeyUp={e => handleKeyUpQuantity(e)} type="text"  
                   />
            <span className="input-group-btn">
             <button className="btn plus"
            onClick={e => handlerUpdateQuantity(1)}>+</button>
             </span>
        </div>

I have multiple instance of the input field but when i try to change the value of input field one other input are updated as well .我有多个输入字段实例,但是当我尝试更改输入字段的值时,另一个输入也会更新。 how can i target a specific input amoung others我如何针对其他人中的特定输入在此处输入图像描述

though i control only input 1 but input 2 also got value虽然我只控制输入 1 但输入 2 也有值

Your two input fields has the same value with {info.quantity}.您的两个输入字段与 {info.quantity} 具有相同的值。 You should change the value and the name to identify input control.您应该更改值和名称以识别输入控件。

for example;例如;

<input maxLength="7" className="form-control" name="quantity1" value={info.quantity1} 
            onChange={e => handleOnChangeQuantity(e)} />
<input maxLength="7" className="form-control" name="quantity2" value={info.quantity2} 
            onChange={e => handleOnChangeQuantity(e)} />
function handleOnChange(e){
    var name = e.target.name;
    var value = e.target.value;

    ...
    setInfo(preInfo => {
      return{
        ...preInfo,
        [name]:value,
        
      }
    })
  }

Easiest way to resolve this is to create a separate counter component which keeps its state locally:解决此问题的最简单方法是创建一个单独的counter组件,将其状态保持在本地:

const Counter = props => {
  const [count, setCount] = useState(0)
  return (
    <div className="counter">
      <button onClick={e => setCount(count - 1)}> - </button>
      <input value={count} onChange={e => setCount(parseInt(e.target.value))} />
      <button onClick={e => setCount(count + 1)}> + </button>
    </div>
  )
}

This way, each component keeps different count.这样,每个组件保持不同的计数。

I don't exactly know your use case but it might make more sense to utilize the same state as an object and keep each value under a specific property name of the object or keep an array as state and the Nth element in the array corresponds to the Nth count - [3, 5, 1, 2, 0, 5] means the first counter has the value 3, the second counter has value 5, the third has value 1 and so on... It all depends on the use case.我不完全了解您的用例,但使用与对象相同的状态并将每个值保留在对象的特定属性名称下或将数组保留为状态并且数组中的第 N 个元素对应于第 N 个计数 - [3, 5, 1, 2, 0, 5]表示第一个计数器的值为 3,第二个计数器的值为 5,第三个计数器的值为 1,依此类推......这完全取决于使用案子。 If I was dynamically rendering each counter, I'd go with an array of values as it is most efficient in terms of memory and performance and a clean solution.如果我动态渲染每个计数器,我会使用一组值,因为它在内存和性能方面是最有效的,并且是一个干净的解决方案。 If I have a fixed number of elements I'd probably go with a combination of object state and separate count component with setState passed down.如果我有固定数量的元素,我可能会结合使用对象状态和单独的计数组件并传递 setState。

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

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