简体   繁体   English

没有在函数内部获取更新的钩子值

[英]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.每当onAnswerCallcalls ,它可以看到的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 不保留任何状态数据,您必须使用useCallbackset* 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.

相关问题 React 钩子值正确呈现但未在 addEventListener 的回调 function 中更新 - React hooks value renders correctly but not updated inside a callback function of addEventListener function 中的变量未更新(使用反应挂钩) - Variable is not updated inside a function (using react hooks) 使用钩子做出反应时没有得到更新的 state 值? - not getting updated state value in react using hooks? React hooks 在 useEffect 的清理函数中丢失了状态变量的更新值 - React hooks loses updated value of state variable inside cleanup function of useEffect “ setInterval”内部的函数无法从钩子接收更新的变量 - Function inside “setInterval” does not recieve updated variables from hooks Function 未使用挂钩指向 state 变量的更新值 - Function not pointing to the updated value of state variable using hooks 出现错误:只能在 function 组件的主体内部调用挂钩 - getting error : Hooks can only be called inside of the body of a function component 反应钩子没有从库事件(FabricJS)的回调 function 中返回更新的 state 值 - React hooks not returning updated state values inside callback function from library events (FabricJS) 在钩子中使用setInterval无法更新日期和时间 - date and time are not getting updated using setInterval in hooks 状态未在 React Hooks 的使用效果中得到更新 - State Not getting Updated In Use Effect of React Hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM