javascript - Why isn't this spread operator updating my state properly?
问题描述
Basically, I'm checking if the user inputted an email or a phone number, and if they didn't I'm trying to change the state of "error". To make things simple, I omitted the "if (!user.email)"/ "if (!user.phone)" statement, even without it the code below doesn't work properly. I can't figure out why.
It will ONLY change the value of the first thing. If I console.log(error) after this code runs, I'll see {phone: true, email: false}. When I switch the order of the setError statements, I'd see {phone: false, email: true}.
const [error, setError] = useState({
email: false,
phone: false,
});
const handleSubmit = (e) => {
e.preventDefault();
setError({ ...error, phone: true });
setError({ ...error, email: true });
}
解决方案
推荐阅读
- javascript - 使用本机 javascript 多点的 Bezier 路径函数
- ruby - 如何在没有 rben 或 rvm 的 ubuntu 16.04 上安装 ruby 2.5.1p57?
- java - 处理 - 自定义字符/文本
- python - 浮点数的 Python 格式最佳实践 - PEP 3101 和其他方法?
- html - 如何在边框和边框内的对象之间添加空间
- python - 如何让python识别从外部文件中随机选择的保存变量
- mysql - Convert mySQL queries to Laravel
- python - 如何在 IDLE 中运行与 Python 安装位置不同的文件夹中的 .py 文件?
- salt-stack - 根据发行版本安装包
- java - 读取配置文件的最佳方式