javascript - 我必须点击两次才能更新我的状态
问题描述
所以基本上,我试图在单击按钮后根据一些搜索参数从 API 调用获得的结果动态呈现表格。我的问题是,当我第一次输入搜索参数并单击搜索按钮时,它工作得很好,但是当我更新搜索参数并单击搜索时,它会显示旧数据,同时,在我的控制台中,我可以看到我的 redux 状态已更新。然后,在更新表格之前,我必须再次单击搜索按钮。
我的代码
const queryResult = useSelector(
(state) => state.certificateReducer.queryResult,
);
const [query, setQuery] = useState([]);
onFinish=()=>{
setQuery(
queryResult?.map((item) => {
return {
actualId: item.id,
id: 1,
certificateNo: item.certificateNumber,
oldCertificateNo: item.oldCertificateNumber,
accountNo: item.holderCscsAccountNumber,
oldAccountNo: item.oldHolderCscsAccountNumber,
shareHolderName: item.holderName,
holderAddress: item.holderAddress,
clientCompany: item.issuingCompanyName,
shareVolume: item.volumeOfBonds,
isSelected: false,
};
}),
);
}
const handleClick = (event) => {
let a = [...query];
a.map((item) => {
if (item.id == event.target.value) {
item.isSelected = !item.isSelected;
}
});
setQuery(a);
};
我在哪里呈现查询:
{query?.map((item, j) => {
return (
<tr key={j}>
<td>{item.id + j}</td>
<td>{item.certificateNo}</td>
<td>{item.oldCertificateNo}</td>
<td>{item.accountNo}</td>
<td>{item.oldAccountNo}</td>
<td>{item.shareHolderName}</td>
<td>{item.holderAddress}</td>
<td>{item.clientCompany}</td>
<td>{item.shareVolume}</td>
<td>
<Button
value={item.id}
onClick={handleClick}
style={{
cursor: 'pointer',
background: '#299F9E 0% 0% no-repeat padding-box',
borderRadius: '50%',
height: '30px',
width: '30px',
padding: '2px',
justifyContent: 'center',
}}>
<img src={NavigationMore} alt="" />
</Button>
</td>
{item.isSelected ? (
<MoreActions
handleSelected={handleSelected}
handleModal={handleModal}
/>
) : null}
</tr>
解决方案
推荐阅读
- javascript - 如何区分同一个git仓库下不同文件夹的变化?
- javascript - 如果没有像 Apollo 或 Relay 这样的库,浏览器客户端如何使用 GraphQL 订阅?
- javascript - Google Script 中的递增日期
- python-3.x - Python3 和 pip3 + 剂量为 python2 安装的轮子也可用于 python3
- flutter - 如何使用 ValueKey 为页面设置键?
- c++ - 无法在 vscode 中编译 FFTW3 库,但相同的命令在 mac 终端中有效
- java - 如何将两个 CSV 与 Json 的字段进行比较
- click - 单击 OpenLayers 中的要素获取更多信息(OpenLayers3 和 GeoServer)
- c - MPI_Gather 不接收数据
- android - 应用程序初始化时只显示白屏