数组状态更新后,React 不渲染页面

问题描述 投票:0回答:1

我有这个数组:

[ {channel1 : false}, {channel2 : true}, {channel3 : false} ]

每次用户单击“保存”时,我都会将所有通道更新为“true”,然后当用户单击“打开”时,它应该打开一个窗口,因为所有通道都是 true。 问题是数组不会立即更新,只有在刷新页面后才更新。 这是代码:

const onSave = async () => {
const data = await saveEspIntegration(identifierConfigurations);
if (!data.success) {
  showUnexpectedErrorMessage();
  closeModal();
  return;
}
showMessage('changesSaved');
handleSave();
};

const handleSave = () => {
availableChannels.map(channelName => { updateChannelConfiguration(channelName, true) 
});
};

const updateChannelConfiguration = (channel: string, isDefineConfiguration: boolean) => {
setChannelConfigurations(prevConfigurations => ({
  ...prevConfigurations,
  [channel]: isDefineConfiguration
}));

};

为什么在我更新数组后 React 没有立即渲染?我使用了扩展(...)运算符,但它仍然没有更新

reactjs react-native react-hooks rendering
1个回答
0
投票

这可能是因为函数

updateChannelConfiguration
仅更新元素布尔值,但您需要更新自身数组,因此它的链接发生了变化

© www.soinside.com 2019 - 2024. All rights reserved.