在`handlechange`函数外部更新一个数组,React JS。

huangapple go评论55阅读模式
英文:

Update a array out side handlechange function React JS

问题

我有一个handleChange函数

const [values, SetValues] = useState({
    addrId: "",
    addrType: "",
    addrLine1: "",
    addrLine2: "",
    countryId: "",
    stateId: "",
    countyId: "",
    zip: "",
  });

 const handleChange = (e) => {
    const { name, value } = e.target;
    SetValues({
      ...values,
      [name]: value,
    });
  };

我尝试使用其他函数(handleAnotherFunction)来更新某个字段(countryID)

const handleAnotherFunction = async (event) => {
 SetValues({ ...values, [values.countryId]: 130 });
}

但是数值没有更新,任何帮助将不胜感激。

英文:

I have an handlechange function

const [values, SetValues] = useState({
    addrId: "",
    addrType: "",
    addrLine1: "",
    addrLine2: "",
    countryId: "",
    stateId: "",
    countyId: "",
    zip: "",
  });

 const handleChange = (e) => {
    const { name, value } = e.target;
    SetValues({
      ...values,
      [name]: value,
    });
  };

I tried to update some field(countryID) using other functions say handleAnotherFunction

const handleAnotherFunction=async(event)=>
 {
 SetValues({ ...values, [values.countryId]: 130 });
}

But value is not updated any help would be appreciated

答案1

得分: 1

此语法:

[name]: value

使用变量 name作为对象键。因此,例如,如果 name 的值是 "foo",那么它将创建或更新键 foo 的值为 value

foo: 'some value'

因此,当您使用相同的语法:

[values.countryId]: 130

它使用 values.countryId作为对象键。因此,如果该例如是 100,那么您正在向对象添加一个名为 100 的键,其值为 130

100: 130

如果您想显式更新字段 countryId,只需使用 countryId

countryId: 130

因此整行将是:

SetValues({ ...values, countryId: 130 });
英文:

This syntax:

[name]: value

uses the value of the variable name as the object key. So, for example, if name has the value "foo" then it creates or updates the key foo to the value of value:

foo: 'some value'

So, when you use this same syntax:

[values.countryId]: 130

It uses the value of values.countryId as the object key. So if that value is, for example, 100 then you're adding a key to the object called 100 and giving it the value 130:

100: 130

If you want to explicitly update the field countryId, just use countryId:

countryId: 130

So the whole line would be:

SetValues({ ...values, countryId: 130 });

答案2

得分: 1

The handleChange 函数在你的代码中是为了根据传入的值更新状态而设计的。它从给定对象的target属性中解构键和值,然后更新状态。所以,如果你想要更新countyId属性,你只需要调用handleChange函数:

handleChange({ target: { name: "countyId", value: 130 } })
英文:

>I tried to update some field(countryID) using other functions

so what's the purpose of creating handleChange ?<br>

The handleChange in your code is designed to update the state based on the given value as parameter. <br>
it destructs the key and the value from the property target of the given object then updates the state. so if you want to update the property countyId all you have to do is to call handleChange :

handleChange({target:{name: &quot;countyId&quot;, value: 130}})

huangapple
  • 本文由 发表于 2023年3月15日 21:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定