Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 在React中,尝试使用setTimeout调用函数延迟_React Native - Fatal编程技术网

React native 在React中,尝试使用setTimeout调用函数延迟

React native 在React中,尝试使用setTimeout调用函数延迟,react-native,React Native,在ReactNative中,我创建了一个加载程序,这个加载程序使用函数显示和隐藏。但是,当我试图在setTimeout中调用此函数时,此函数不起作用,它会返回如下错误->this.showLoader不是函数。没有定义。 但当我尝试在没有设置超时的情况下运行时,它运行良好 export default class ThirdScreen extends Component<Props> { constructor(props) { super(props)

在ReactNative中,我创建了一个加载程序,这个加载程序使用函数显示和隐藏。但是,当我试图在setTimeout中调用此函数时,此函数不起作用,它会返回如下错误->this.showLoader不是函数。没有定义。 但当我尝试在没有设置超时的情况下运行时,它运行良好

export default class ThirdScreen extends Component<Props> {

  constructor(props) {
        super(props)

    this.state = 
    {
       isLoading: false
    }
    this.showLoader = this.showLoader.bind(this);
  }

  componentDidMount() {

    setTimeout(function(){
      this.showLoader()
    }, 1000);
     //this.showLoader()
    }

    showLoader () {
      this.setState({ isLoading: true });
    }

    hideLoader = () => {
      this.setState({ isLoading: false });
    }
}
导出默认类ThirdScreen扩展组件{
建造师(道具){
超级(道具)
此.state=
{
孤岛加载:false
}
this.showLoader=this.showLoader.bind(this);
}
componentDidMount(){
setTimeout(函数(){
this.showLoader()
}, 1000);
//this.showLoader()
}
showLoader(){
this.setState({isLoading:true});
}
hideLoader=()=>{
this.setState({isLoading:false});
}
}

看起来showLoader()函数是在componentDidMount()内声明的,因此无法通过此指针访问。

看起来showLoader()函数是在componentDidMount()内声明的,因此无法通过此指针访问。

希望这能解决您的问题

setTimeout(()=> this.showLoader(), 1000)

希望这能解决你的问题

setTimeout(()=> this.showLoader(), 1000)