简体   繁体   中英

Is there a shorter code for useState const?

I am using react-native useState and have following const in the code. 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]

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. In my opinion its as short as it can be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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