React native 如何在另一个函数中清除setTimer

React native 如何在另一个函数中清除setTimer,react-native,React Native,组件正在调用另一个文件中的函数,setTimer用于更新变量: timer.js const timer = () => { this.timeout = setTimeout(() => { pressTwice = false; }, duration); }; timerUI.js componentDidMount() { timer(); } componentWillUnmount() { clearTimeout(this.t

组件正在调用另一个文件中的函数,setTimer用于更新变量:

timer.js

const timer = () => {
  this.timeout = setTimeout(() => {
    pressTwice = false;
  }, duration);
};
timerUI.js

componentDidMount() {
    timer();
  }

  componentWillUnmount() {
    clearTimeout(this.timeout);// how to clean the this.timeout?
  }
我得到这个错误:

Can't perform a React state update on an unmouted component.
数据(即状态和道具)通常沿react组件树“向下”流动,即从父级到子级。如果孩子的兄弟姐妹需要访问其级别或级别以下的数据,解决方案是将数据“放样”到最近的共同祖先(在本例中为父级)。换句话说,您需要跟踪父组件中的计时器,以便不同的子组件可以通过道具接收对它的引用以取消它

希望这能说明it概念:

母公司

const setTimer = () => {
  this.timeout = setTimeout(() => {
  pressTwice = false;
}, duration);

render() {
  return (
    <>
      <ChildSetsTimer setTimer={this.setTimer} />
      <ChildClearsTimer timerRef={this.timeout} />
    </>
  );
}
const setTimer=()=>{
this.timeout=setTimeout(()=>{
按两次=false;
},持续时间);
render(){
返回(
);
}
数据(即状态和道具)通常“向下”反应组件树,即父级到子级。如果子级的兄弟需要访问其级别或以下的数据,解决方案是“放样”该数据将指向最近的公共祖先,即本例中的父级。换句话说,您需要跟踪父级中的计时器,以便不同的子组件可以通过道具接收对其的引用以取消它。