简体   繁体   English

useState const 是否有更短的代码?

[英]Is there a shorter code for useState const?

I am using react-native useState and have following const in the code.我正在使用 react-native useState 并在代码中有以下 const 。 Was wondering if there is a shorter way to write this, since they are very similar.想知道是否有更短的方法来写这个,因为它们非常相似。

const [oldPasswordValue, setOldPasswordValue] = useState('');
const [newPasswordValue, setNewPasswordValue] = useState('');
const [confirmPasswordValue, setConfirmPasswordValue] = useState('');

you could do something like that你可以做这样的事情

const oldPassword = useState('');

and when you try to read the value write this oldPassword[0] and if you want to update the value get the second index oldPassword[1]当您尝试读取该值时,请写入此oldPassword[0] ,如果您想更新该值,请获取第二个索引oldPassword[1]

here is a simple example这是一个简单的例子

 let data = [1, 2] // first way with destructring let [el1, el2] = data; console.log(el1, el2) // second way by indexing console.log(data[0], data[1])

Im not rly sure if there is any shorter version, or how would you even be posible to have shorter, you need value and setValue, also default state.我不确定是否有任何更短的版本,或者你甚至可能有更短的版本,你需要 value 和 setValue,也默认为 state。 In my opinion its as short as it can be.在我看来,它尽可能短。

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

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