简体   繁体   English

如何使用 UseState 钩子作为 object 中的属性名称?

[英]How to use the UseState hook as the name of a property in an object?

In my project, i want to make an axios post request.在我的项目中,我想发出 axios 发布请求。

I want that if the useState hook is set to true , the name of the property is different , than if it is set to false .我希望如果useState挂钩设置为true ,则属性名称与设置为false不同

For example:例如:

const [state, setState] = useState(); //like useEmail

const object ={
property1 like name: someProperty,                      //like name value
property2 like email/phonenumber: someOtherproperty    //like email/phonenumber value
}

setState(true)// object property2 = email
setState(false)//object property2 = number

Is it possible?可能吗? Thanks for your help!谢谢你的帮助!

const [state, setState] = useState(false);

const object = {
  [state ? 'email' : 'name']: 'something'
}

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

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