简体   繁体   English

如何在反应 typescript 挂钩中更改多个输入的值

[英]How to change the value of multiple inputs in react typescript hooks

I am new to the react typescript world and I am currently trying to change the state of an object keys values with only one onChangeHandler, I was searching a lot and all that I could see was the same solution but it doesn't help, my current input fields are staying empty tho the event is triggered.我是反应 typescript 世界的新手,我目前正在尝试更改 object 键值的 state触发事件时,当前输入字段保持为空。 I would appreciate the help.我将不胜感激。

    const [editAddress, setEdditAddres] = useState({
    houseNumber: '',
    houseName: '',
    flatNumber: '',
    street: '',
    addressLine2: '',
    town: '',
    postcode: '',
  });

  const handleInputChange = (e) => {
    const { name, value } = e.target;
    setEdditAddres({
      ...editAddress,
      [name]: value,
    });
  };
<form>
            <TextInput
              label="House number"
              className="OrderDeliveryDetails__houseNumber"
              name="houseNumber"
              value={editAddress.houseNumber}
              id="houseNumber"
              error=""
              onChange={handleInputChange}
            />
            <TextInput
              label="House name"
              className="OrderDeliveryDetails__houseName"
              name="houseName"
              value={editAddress.houseName}
              id="houseName"
              error=""
              onChange={handleInputChange}
            />
            <TextInput
              label="Flat number"
              className="OrderDeliveryDetails__flatNumber"
              name="flatNumber"
              value={editAddress.flatNumber}
              id="flatNumber"
              error=""
              onChange={handleInputChange}
            />
            <TextInput
              label="Street (optional)"
              className="OrderDeliveryDetails__street"
              name="street"
              value={editAddress.street}
              id="street"
              error=""
              onChange={handleInputChange}
            />
            <TextInput
              label="Address line 2 (optional)"
              className="OrderDeliveryDetails__addressLine2"
              name="addressLine2"
              value={editAddress.addressLine2}
              id="addressLine"
              error=""
              onChange={handleInputChange}
            />
            <TextInput
              label="Town or city"
              className="OrderDeliveryDetails__town"
              name="town"
              value={editAddress.town}
              id="town"
              error=""
              onChange={handleInputChange}
            />
            <TextInput
              label="Postcode"
              className="OrderDeliveryDetails__postcode"
              name="postcode"
              value={editAddress.postcode}
              id="postcode"
              error=""
              onChange={handleInputChange}
            />
          </form>

I was able to fix it, the problem was hiding in the component which has no prop name and that leads to not being able to change the current value of an input, I saw this after I changed the component to a normal vanilla js and then it worked.我能够修复它,问题隐藏在没有道具名称的组件中,导致无法更改输入的当前值,我在将组件更改为普通的 vanilla js 之后看到了这个,然后有效。 So for future references just to know, check your props on your components.因此,为了了解以后的参考资料,请检查组件上的道具。 Thank you all for the help.谢谢大家的帮助。

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

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