[英]Not getting updated hooks value inside function
Not getting updated value of state in function which gets triggered by eventListener.未在由 eventListener 触发的函数中获取状态的更新值。 Even tried with background timer.
甚至尝试使用后台计时器。 What would be the problem?
会有什么问题?
displayIncomingCall() // this function gets called from UI.
const callApp = () => {
const [calls, setCalls] = useState({});
const addCall = (key, value) => {
setCalls({ ...calls, [key]: value });
};
const displayIncomingCall = number => {
const callUUID = getCurrentCallId();
addCall(callUUID, number);
...
};
const onAnswerCall = () => {
console.log('=== onAnswerCall ::: ===', calls); <--- always initial value. Not getting updated one.
}
useEffect(() => {
console.log('=== useEffect ::: ===', calls); // getting updated value
}, [calls]);
useEffect(() => {
RNCallKeep.addEventListener('answerCall', onAnswerCall);
return () => {
RNCallKeep.removeEventListener('answerCall', onAnswerCall);
};
}, []);
console.log('=== parent ::: ===', calls); // getting updated value
return (...)
}
onAnswerCall
is only referenced in the useEffect
callback, and the useEffect
callback has an empty dependency array, so it only runs on the initial mount. onAnswerCall
只在useEffect
回调中被引用,而useEffect
回调有一个空的依赖数组,所以它只在初始挂载上运行。 Whenever onAnswerCall
is called, the binding of calls
that it can see is in an old closure.每当
onAnswerCall
被calls
,它可以看到的calls
绑定在一个旧的闭包中。
Use a ref instead of state:使用 ref 而不是 state:
const callsRef = useRef({});
const addCall = (key, value) => {
callsRef.current[key] = value;
};
const onAnswerCall = () => {
console.log('=== onAnswerCall ::: ===', callsRef.current);
}
If calls
needs to be in state so that its change results in a re-render, then you can either use both state and a ref, and assign to the ref when you call setCalls
, or you can remove the dependency array from useEffect
, thus adding and removing the listeners to RNCallKeep
on each render.如果
calls
需要处于状态以便其更改导致重新渲染,那么您可以同时使用状态和引用,并在调用setCalls
时分配给引用,或者您可以从useEffect
删除依赖数组,因此在每次渲染时向RNCallKeep
添加和删除侦听器。
React hooks state does not persist any state data, you have to use useCallback
and the previous state privided in the set*
state functions to keep up with your state: React hooks state 不保留任何状态数据,您必须使用
useCallback
和set*
state 函数中的先前状态来跟上您的状态:
const callApp = () => {
const [calls, setCalls] = useState({});
const addCall = (key, value) => {
// Use previous state provided to update the state
setCalls(calls => ({ ...calls, [key]: value }));
};
const displayIncomingCall = number => {
const callUUID = getCurrentCallId();
addCall(callUUID, number);
...
};
// Use useCallback to keep up with the state
const onAnswerCall = useCallback(() => {
console.log('=== onAnswerCall ::: ===', calls); <--- Will get updated
}, [calls]);
useEffect(() => {
console.log('=== useEffect ::: ===', calls); // getting updated value
}, [calls]);
useEffect(() => {
RNCallKeep.addEventListener('answerCall', onAnswerCall);
return () => {
RNCallKeep.removeEventListener('answerCall', onAnswerCall);
};
}, [onAnswerCall]);
console.log('=== parent ::: ===', calls); // getting updated value
return (...)
}
Your event listener callback is closed over the initial state of calls
.您的事件侦听器回调在
calls
的初始状态下关闭。 Add it as a dependency to the effect, and you should register a new listener (and remove old ones) each time its value changes.将其添加为效果的依赖项,并且每次其值更改时您都应该注册一个新的侦听器(并删除旧的侦听器)。
EDIT: In reality, you should move the entire onAnswerCall
function into the effect, since it is only used there:编辑:实际上,您应该将整个
onAnswerCall
函数移动到效果中,因为它只在那里使用:
useEffect(() => {
const onAnswerCall = () => {
console.log('=== onAnswerCall ::: ===', calls);
};
RNCallKeep.addEventListener('answerCall', onAnswerCall);
return () => {
RNCallKeep.removeEventListener('answerCall', onAnswerCall);
};
}, [calls]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.