reactjs - react-native-background-timer,null 不是对象
问题描述
我使用 react-native-background-timer 时出错。如果您能帮我解决这个问题,我将不胜感激。
我在Expo Snack上开发了一个手机app,现在想实现自动删除账号功能:当一个账号被创建5分钟没有被验证时,它会被自动删除。所以,我搜索了后台计时器,我找到了下面的库。
https://github.com/ocetnik/react-native-background-timer
但是,由于以下错误,我无法实现
(3:2693) null is not an object (evaluating 'o.setTimeout')
这是我的代码
import React, { Component } from 'react';
import { View, Text, TouchableOpacity, Platform } from 'react-native';
import BackgroundTimer from 'react-native-background-timer';
let counter = 0;
let timer = null;
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
second: 0,
};
}
_interval: any;
onStart = () => {
if (Platform.OS == 'ios') {
BackgroundTimer.start();
}
this._interval = BackgroundTimer.setInterval(() => {
this.setState({
second: this.state.second + 1,
});
}, 1000);
};
onPause = () => {
BackgroundTimer.clearInterval(this._interval);
};
onReset = () => {
this.setState({
second: 0,
});
BackgroundTimer.clearInterval(this._interval);
};
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity onPress={this.onStart}>
<Text>start</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.onPause}>
<Text>pause</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.onReset}>
<Text>reset</Text>
</TouchableOpacity>
<Text>{this.state.second}</Text>
</View>
);
}
}
我跟着这个家伙的教程 https://medium.com/@loons.create/how-to-setup-react-native-background-timer-22263d655847
配备的功能,javascript 的 setInterval 等当然可以作为计时器正常工作,但实际上它们在 react native 中并不能工作。
我错过了什么,或者这是这个库中的一个问题(我想是这样)?如果是这样,请告诉我这个库的可用版本;我使用最新版本 2.2.0 和 React v35.0.0
谢谢
解决方案
您不能在托管工作流程中将“react-native-background-timer”与 Expo 一起使用。这个库需要编译一些本机代码。
相反,您应该参加 Expo BackgroundFetch,它正在做几乎相同的事情。
https://docs.expo.io/versions/latest/sdk/background-fetch/
使用 Expo 组件,您无需弹出或编译额外的本机代码。
推荐阅读
- git - Git rebase order - 混乱
- c-preprocessor - 如何在编译期间使用 C 预处理器显示条件错误?
- c - C中的阶乘递归(分段错误)
- d - 将 std.algorithm.iteration.sum 与 Duration[] 一起使用
- amazon-web-services - 在父 AWS 主域上创建的子域不起作用
- android - 在滑动时上下滑动片段
- java - Maven POM file for running tests(not JUnit)
- java - How to implment SpanQuery with MultiFieldQuery in java using lucene
- c# - 将字符串数组与“FileInfo / GetFiles”进行比较
- java - 将循环遍历数组java中的条目的while循环