javascript - 未从 componentDidMount 方法调用函数
问题描述
问题是没有从 componentDidMount 方法调用 _requestPermission 函数。
componentDidMount() {
_requestPermission = () => {
Permissions.request('storage').then(response => {
// Returns once the user has chosen to 'allow' or to 'not allow' access
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
console.log(response)
})
}
}
我已经尝试将其从 componentDidMount 中删除并使用按钮触发它。在此之后,该功能按预期启动并请求了存储权限。如何在渲染此组件时调用此函数?我对任何解决方案持开放态度,而不仅仅是从 componentDidMount 触发它。
这是我整个组件的代码:
import React, {Component} from 'react';
import {StyleSheet, View} from 'react-native'
import { RNCamera } from 'react-native-camera'
import { CameraRoll } from 'react-native'
import Permissions from 'react-native-permissions'
import ActionButton from 'react-native-action-button'
import Icon from 'react-native-vector-icons/Ionicons'
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
button: {
height: 200,
justifyContent: 'flex-end',
alignItems: 'center'
},
actionButtonIcon: {
fontSize: 20,
height: 22,
color: 'white',
},
});
export default class Cam extends Component {
constructor() {
super()
this.takePicture = this.takePicture.bind(this)
}
takePicture = async function() {
if (this.camera) {
const options = { quality: 0.5, base64: true }
const data = await this.camera.takePictureAsync(options)
CameraRoll.saveToCameraRoll(data.uri)
}
}
componentDidMount() {
_requestPermission = () => {
Permissions.request('storage').then(response => {
// Returns once the user has chosen to 'allow' or to 'not allow' access
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
console.log(response)
})
}
}
render() {
return (
<View style={styles.container}>
<RNCamera
ref={ref => {this.camera = ref}}
style={{
flex: 1,
width: '100%',
position: 'relative'
}}
>
</RNCamera>
<ActionButton size={80} useNativeFeedback={false} buttonColor="rgba(231,76,60,1)">
<ActionButton.Item useNativeFeedback={false} buttonColor='#9b59b6' title="Settings" onPress={this.props.switchScreen}>
<Icon name="md-create" style={styles.actionButtonIcon} />
</ActionButton.Item>
<ActionButton.Item useNativeFeedback={false} buttonColor='#1abc9c' title="Permission" onPress={this._requestPermission}>
<Icon name="md-done-all" style={styles.actionButtonIcon} />
</ActionButton.Item>
<ActionButton.Item useNativeFeedback={false} buttonColor='#1abc9c' title="Start" onPress={this.takePicture}>
<Icon name="md-done-all" style={styles.actionButtonIcon} />
</ActionButton.Item>
</ActionButton>
</View>
)
}
}
解决方案
您正在为 分配一个函数_requestPermission
,但实际上从未调用它。而是直接调用Permissions.request
,例如
componentDidMount() {
Permissions.request('storage').then(response => {
// Returns once the user has chosen to 'allow' or to 'not allow' access
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
console.log(response)
})
}
}
推荐阅读
- swift - 在顶部时更改 UITableView 部分标题
- python - 我们如何使用 pandas 连接两个没有公共列的不同表
- sql - 有没有办法为 FullCalendar 执行添加编辑删除读取?
- java - Java EE 7:按名称模式注入所有 bean 实例
- c# - 更新 JSON 文件而不是添加
- javascript - React-native - 使用抽屉导航器传递参数
- shopify - Shopify - 对收藏列表进行分页
- java - PowerMockito verifyNew withArguments 对象和对象数组
- android - 在android中使用Retrofits2将文件上传到服务器
- python - Python将来自while循环的元组加入列表