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