没有从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)

      })

    }  

  }



RISEBY
浏览 207回答 1
1回答

牧羊人nacy

您正在为分配一个函数_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)      })    }   }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript