useState to update multiple values in React

前端 未结 2 2021
自闭症患者
自闭症患者 2021-02-14 07:23

I\'ve a series of user data elements which I\'m collecting inside a React component using hooks.

const [mobile, setMobile] = useState(\'\');
const [username, set         


        
2条回答
  •  不要未来只要你来
    2021-02-14 07:27

    You should add name attributes to input tags. Each name must refer to key in AllValues object.

    const [allValues, setAllValues] = useState({
       mobile: '',
       username: '',
       email: '',
       password: '',
       confirmPassword: ''
    });
    const changeHandler = e => {
       setAllValues({...allValues, [e.target.name]: e.target.value})
    }
    return (
       
       // ...
    )
    

提交回复
热议问题