react hooks 太多的useState 显得很冗余,有没有什么规整的方法呢?

react

import React, { useState } from 'react'

export default function Test() {

const [state, setState] = useState({

username: 'admin',

password: '123456',

})

const handleInput = (e, field) => {

setState((prevState) => {

return { ...prevState, [field]: e.target.value }

})

}

const handleLogin = () => {

const { username, password } = state

console.log(username, password)

}

return (

<div>

<div>

<input

value={state.username}

onChange={(e) => handleInput(e, 'username')}

></input>

</div>

<div>

<input

value={state.password}

onChange={(e) => handleInput(e, 'password')}

></input>

</div>

<button onClick={handleLogin}>登录</button>

</div>

)

}

以上是 react hooks 太多的useState 显得很冗余,有没有什么规整的方法呢? 的全部内容, 来源链接: utcz.com/z/384356.html

回到顶部