我有以下代码
const initialState = {
values: {
email: '',
password: ''
},
touched: {
email: false,
password: false
},
errors: false,
isValid: false,
isLoading: false,
submitError: null
};
function SignIn(props) {
const [state, setState] = useState(initialState);
useEffect(() => {
console.log(state);
});
const handleFieldChange = (field, value) => {
const newState = state;
newState.submitError = null;
newState.touched[field] = true;
newState.values[field] = value;
newState.errors = true; // set to true just for test
setState(newState);
};
const showEmailError = state.errors.email;
return (
<div>
<TextField
className={classes.textField}
label="Email address"
name="email"
onChange={event => handleFieldChange('email', event.target.value)}
type="text"
value={state.email}
variant="outlined"
/>
{showEmailError && (
<Typography
className={classes.fieldError}
variant="body2"
>
{state.errors.email[0]}
</Typography>
)}
</div>
)
}
我遇到的问题是,在handFieldChange执行时,没有出现电子邮件错误。就好像我没有在我的 return 语句中得到最新的状态变化。我做错了什么?
一只斗牛犬
一只名叫tom的猫
相关分类