reactjs - 为什么在 Redux 动作触发后 react 不会设置状态?
问题描述
我试图在 redux 触发后在组件中本地设置状态,但它不起作用,我在这里做错了什么?
<TableCell
onClick={() => {
if (dropDownLocation === "allLocations") {
overlayDisplay("open");
} else {
console.log(values.rescheduleDate);
getReservationsByDate(row.reservation_date); // Redux action
getStaffMember(row.staff_name); // Redux action
setValues((prevState) => {
return {
...prevState,
rescheduleDate: true, // always logged as false
};
});
}
}}
>CLICK</TableCell>
解决方案
默认情况下,Redux 的 action 是同步调度的,所以这里可以使用 async-await 来做异步操作。
<TableCell
onClick={async () => {
if (dropDownLocation === "allLocations") {
overlayDisplay("open");
} else {
console.log(values.rescheduleDate);
await getReservationsByDate(row.reservation_date); // Redux action
await getStaffMember(row.staff_name); // Redux action
await setValues((prevState) => {
return {
...prevState,
rescheduleDate: true, // always logged as false
};
});
}
}}
>CLICK</TableCell>
推荐阅读
- python - 从数据库 Django 向表中添加删除原始按钮
- sql - 在锁步中取消嵌套存储为文本的多个数组列
- python - Django Annotate - 获取注释中的数字列表
- django - 一个包含多个 django 项目的数据库
- r - 用于绘图的阴影图形设备
- javascript - 当 ref 的值发生变化时,Vue 不会更新模板 Vue 3
- python - 获取列数据框中给定复杂条件的索引
- unit-testing - Visual Studio Enterprise 2019,“配置文件”单元测试导致错误:文件不包含数据缓冲区
- powershell - 如何从 https://www.googleapis.com/oauth2/v3/certs 生成符合 PEM 或 x509 的证书?
- python - python中的模板化对象生成