为了账号安全,请及时绑定邮箱和手机立即绑定

没有从componentDidMount方法调用该函数

没有从componentDidMount方法调用该函数

RISEBY 2021-05-03 20:03:48
问题是没有从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)      })    }    }
查看完整描述

1 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

您正在为分配一个函数_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)

      })

    } 

  }


查看完整回答
反对 回复 2021-05-13
  • 1 回答
  • 0 关注
  • 206 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信