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



在 ReactNative 我创建了一个加载器,这个加载器使用 by 函数显示和隐藏。但是当我尝试在 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 });
}
}

希望这将解决您的问题

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

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

最新更新