我有以下 JSX 代码:
<FormGroup>
<Label for="exampleEmail" style={{fontWeight:"bold"}}>What is your e-mail address?</Label>
<Input type="email" name="email" id="exampleEmail" onChange={(e)=>setStateForProperties(e)} />
</FormGroup>
<FormGroup>
<Label for="examplePassword" style={{fontWeight:"bold"}}>What is your password?</Label>
<Input type="password" name="password" id="examplePassword" onChange={(e)=>setStateForProperties(e)}/>
</FormGroup>
状态声明 -
const iLoginCreds ={
userName:'',
password:''
}
const [loginCreds, setLoginCredentials] = useState(iLoginCreds)
onChange方法改变状态 -
function setStateForProperties(e)
{
alert(e.target.id);
switch (e.target.id) {
case 'exampleEmail':
setLoginCredentials(...loginCreds,{
userName:e.target.value
});
break;
case 'examplePassword':
setLoginCredentials(... loginCreds,{
password:e.target.value
});
default:
break;
}
console.log(loginCreds);
}
错误 -
每当我在文本框中写任何内容("onchange" 事件触发),都会收到一个错误 -
TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))