简体   繁体   English

如何仅更改包含对象的 React useState 中的一个值并将其他值分配给其他值?

[英]How can I change only one value in a React useState that contains an object and assign a different one to the rest?

I have the following State:我有以下状态:

const [clickColumn, setClickColumn] = useState({
    name: 0,
    tasks: 0,
    partner: 0,
    riskFactor: 0,
    legalForm: 0,
    foundationYear: 0
  })

And now, for example, I only want to set name to 2 and the rest to 1. But I do not want to write it this way: setClickColumn({ name: 2, tasks: 1, riskFactor: 1, partner: 1, legalForm: 1, foundationYear: 1 });现在,例如,我只想将 name 设置为 2,其余设置为 1。但我不想这样写: setClickColumn({ name: 2, tasks: 1, riskFactor: 1, partner: 1, legalForm: 1, foundationYear: 1 });

How can I make it shorter?我怎样才能让它更短?

You can use spread operator to make it simple:您可以使用扩展运算符使其变得简单:

setClickColumn({ ...clickColumn, name: 2 });

it spreads the existing object clickColumn and adds the name: 2 to it, overriding the default value.它传播现有对象 clickColumn 并添加名称:2,覆盖默认值。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Making it shorter wouldn't necessarily make it more simple, but you can do something like this:缩短它并不一定会使它更简单,但您可以执行以下操作:

 const obj = { name: 0, tasks: 0, partner: 0, riskFactor: 0, legalForm: 0, foundationYear: 0 }; const entries = Object.entries(obj).map(([key, value]) => key === 'name' ? [key, 2] : [key, 1]); const result = Object.fromEntries(entries); console.log(result);

You can easily create a function that will reset the state for you by parsing the object and give the right value:您可以轻松创建一个函数,通过解析对象并给出正确的值来为您重置状态:

 const getResetClickColumn = (clickColumn) => { Object.keys(clickColumn).forEach(key => { const value = (key === 'name' ? 2 : 1); clickColumn[key] = value; }) return clickColumn; } const reset = getResetClickColumn({ name: 0, tasks: 0, partner: 0, riskFactor: 0, legalForm: 0, foundationYear: 0 }) console.log(reset)

In addition to @MorKadosh 's answer:除了@MorKadosh 的回答:

setClickColumn(Object.fromEntries(Object.entries(clickColumn)
   .map(([key, value]) => [key, key === 'name' ? 2 : 1])))

You can simply make a temp variable您可以简单地制作一个临时变量

let tmpColumn = clickColumn;
tmpColumn.name = 2;
// other changes
setClickColumn(tmpColumn);

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

相关问题 如何使用 useState React 正确更改 state object 值之一 - How to change one of the state object value correctly with useState React 如何在 React useState 中存储和更新多个值? 如果其中一个值在数组中? - How can I store and update multiple values in React useState? if one of the value is in array? 如何在反应中更改 useState 的 object 值? - How to change a useState's object value in react? 在 React Hook 中,如何更改 useState 数组的特定值? - In React Hook, how can I change specific value of useState array? React Hooks,useState 相关问题。 如何更新 object 的一个属性并保留 rest - React Hooks, useState related question. How to update one property of object and keep the rest "如何在反应中使用 useState 更改幻灯片?" - How can I change slides with useState in react? 如何检查对象是否包含至少一个其值包含JavaScript子字符串的键? - How can I check if an object contains at least one key whose value contains a substring in JavaScript? 如何在对象数组中仅 rest 的 object 的一个值 - How to rest only one value of an object inside an array of Objects 反应,我如何只更新使用 useState 钩子的 object 中的一个属性? - React, how do I update just one property in an object that uses the useState hook? 如何从useState中的不同字符串合并为一个object? - how to merge into one object from different strings in useState?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM