我有一个像这样的react组件,在这里我设置了一个WebSocket,在WebSocket事件中,我试图更新Redux内部的状态
const Body = () => {
// Hook for Redux dispatch
const dispatch = useDispatch();
// REDUX reducers
const workCenterAvailabilityQueue = useSelector(selectAllWorkCenterAvailabilities)
// Add or Update workCenterAvailability
let existingWorkCenter;
const workCenterAvailabilityHandler = (id, data) => {
console.log('id = ', id)
console.log('data = ', data)
console.log(workCenterAvailabilityQueue)
existingWorkCenter = workCenterAvailabilityQueue.find(bm => bm.WorkCenter.WorkCenter_ID === id);
console.log(existingWorkCenter)
if (existingWorkCenter) {
console.log('WorkCenter exists, updating data!')
dispatch(updateWorkCenterAvailability({
id: existingWorkCenter.id,
changes: { Connection_Status: data.Connection_Status }})
);
}
else {
console.log('WorkCenter does not exist, creating one!')
dispatch(addWorkCenterAvailability(data));
}
}
// Debugging
useEffect(() => {
console.log('Updated state:', workCenterAvailabilityQueue);
if(workCenterAvailabilityQueue[10]){
console.log('workCenterAvailabilityQueue[10] -> ', workCenterAvailabilityQueue[10])
}
console.log('\n')
}, [workCenterAvailabilityQueue]);
// Web Socket setup
useEffect(() => {
console.log('connecting to Flask socket')
const socket = io('http://localhost:5000');
console.log('Start listening')
socket.on('workCenterAvailability', (data) => {
workCenterAvailabilityHandler(data.WorkCenter.WorkCenter_ID, data)
});
console.log('emitting message')
socket.emit('requestWorkCenterAvailability');
console.log('emitted!')
// Clean up function
return () => {
socket.disconnect();
};
}, []);
字符串
所以我的问题是,当我尝试使用处理函数时,workCenterAvailabilityQueue
总是作为一个空数组返回,所以当我尝试在队列中查找对象时,它总是失败
然而,基于我一直放在底部用于调试目的的useEffect钩子,我可以看到每次我向队列添加东西时,它实际上都被添加到Redux中
为什么我的workCenterAvailabilityQueue
不包含实时信息?
1条答案
按热度按时间mo49yndu1#
第二个
useEffect
钩子在"workCenterAvailability"
事件回调处理程序中调用workCenterAvailabilityHandler
,该事件回调处理程序在选定的workCenterAvailabilityQueue
状态上具有过时的JavaScript闭包。您可以通过以下几种方式解决此问题:
1.导入store对象并在
workCenterAvailabilityHandler
回调处理程序中获取当前状态值。字符串
1.使用React ref缓存当前的
workCenterAvailabilityQueue
状态值。型