react-native - react-native-camera 条形码扫描仪冻结,因为它扫描速度太快
问题描述
我正在尝试使用react-native-camera
. 首先,它扫描二维码并提取一个字符串,然后导航到下一个屏幕react-navigation
。在第二个屏幕中,它进行 API 调用。
现在,如果我返回扫描仪屏幕,将立即扫描二维码。那就是我遇到错误并且扫描仪冻结的地方。我通常会收到此错误:
Can't call setState (or forceUpdate) on an unmounted component
我认为这是因为我的componentWillUnmount
清理工作不正常或不够快,但我已经取消了 axios 请求。
requestCode = (code) => {
if (cancel != undefined) {
cancel();
}
axios.get(API_URI + code, {
cancelToken: new CancelToken(function executor(c) {
cancel = c;
})
}).then(response => {
console.log(response)
//checks if code was already called
this.checkUsed(response.data)
})
.catch(error => {
this.setState({ isValid: false })
});
}
componentWillUnmount() {
cancel();
}
也许我可以稍后安装相机扫描仪,这样它就不会扫描得这么快,或者它甚至可能是 React Navigation 的错误?
解决方案
您可以使用标志来控制。
class QR extends Component {
constructor(props) {
super(props)
this.state = {
scanable: true
}
this.cameraAttrs = {
ref: ref => {
this.camera = ref
},
style: styles.preview,
type: RNCamera.Constants.Type.back,
barCodeTypes: [RNCamera.Constants.BarCodeType.qr],
onBarCodeRead: ({ data }) => {
this.callback(data)
}
}
}
componentWillMount() {
this._mounted = true
}
componentWillUnmount() {
this._mounted = false
}
callback(text) {
if (!this.state.scanable) {
return
}
console.log(text)
this.setState({ scanable: false })
setTimeout(() => {
if (this._mounted) {
this.setState({ scanable: true })
}
}, 1000) // 1s cooldown
}
render() {
return (
<View style={styles.container}>
<RNCamera
{...this.cameraAttrs}
>
</RNCamera>
</View>
)
}
}
推荐阅读
- batch-file - 自动检查批处理文件中的输入
- python - Python Pandas 使用列首字符和一组要匹配的值来查找与模式匹配的行
- java - 将给定日期与给定时区中没有时间部分的当前日期进行比较的最佳方法
- python - 如何用排名替换 Pandas 行中的值
- reactjs - 如何运行 Express-React 项目
- node.js - 找不到模块 [Yarn 环境变量]
- javascript - 拒绝后承诺链继续
- c# - 如何使用 Windows 窗体中的按钮控件更改在类级别定义的整数数组?
- java - 带有 json 数据的 Java 请求
- python - 使用 Python 按列值将大文本文件拆分为较小的文本文件