【问题标题】:Unexpected token, expected ";" in react native意外的令牌,应为“;”在本机反应
【发布时间】:2020-06-17 12:08:14
【问题描述】:

我是 React Native 的新手,我正在开发一个视频应用程序来帮助我的学习曲线。在下面的代码中,我已尽我所能解决“displayModal”行上的错误,但无法解决。请任何人都可以帮我解决这个问题。 我想在图像/视频捕获时将其显示在模态中,并且从模态中我将能够“丢弃”或“保存”(到 Firebase)或“共享”图像/视频。

import React from 'react';
import { View, Text, Image, Modal, TouchableOpacity } from 'react-native';
import { Ionicons } from '@expo/vector-icons';

import styles from './styles';

export default ({captures=[]}) => {
   state = {
   isVisible: false
   }
 // hide show modal
  displayModal(show){  ------this is where am getting the error
   this.setState({isVisible: show})
}
 return (

  <Modal 
   transparent={true}
   visible={this.state.isVisible}
   // style={[styles.bottomToolbar, styles.galleryContainer]} 
>
 <View style={{backgroundColor: "#000000aa", flex: 1}}>
  {captures.map(({ uri }) => (
  <View style={styles.galleryImageContainer} key={uri}>
   <Image source={{ uri }} style={styles.galleryImage} />
  </View>
 ))}
 </View>
 <TouchableOpacity style={{justifyContent: 'center', alignItems: 'center'}}>
 <Ionicons
  name="close-outline"
  color="white"
  size={20}
  onPress={() => {this.displayModal(!this.state.isVisible);}}
  />
   <Text>Discard</Text>
   </TouchableOpacity>
   </Modal>

  );
};

click here to see error image

【问题讨论】:

  • 您正在使用功能组件,但仍在使用 this,它仅在类中使用。

标签: reactjs react-native expo react-native-component


【解决方案1】:

从您的代码来看,它看起来像 functional component,但您将 state 用作 class-based component,这可能是您收到错误的原因:

export default ({captures=[]}) => {
  state = {
    isVisible: false
  }
  // hide show modal
  displayModal(show){  ------this is where am getting the error
    this.setState({isVisible: show})
  }

上面的代码块应该是这样的:

export default ({captures=[]}) => {

  const [state,setState] = useState({ isVisible: false })

  // hide show modal
  const displayModal = (show) => { 
    setState({isVisible: show})
  }

【讨论】:

  • 感谢 Vivek,它可以工作,但我如何使用 onPress 调用函数来关闭模态......设置 visible={true}
【解决方案2】:

您正在将功能组件与类组件混合。 “this.state”和“this.setState”属于类组件,其余都属于功能组件。

尝试改变这一点:

state = {
   isVisible: false
   }
 // hide show modal
  displayModal(show){  ------this is where am getting the error
   this.setState({isVisible: show})
}

到这里:

const [isVisible, setIsVisible] = React.useState(false);

const displayModal = show => setIsVisible(show);

另外,在return语句中,去掉字符串/单词“this”和“this.state”。

请求添加:

import React, { useState } from 'react';
import { View, Text, Image, Button, Modal, TouchableOpacity } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { storage } from './fbstorage';
import { Camera } from 'expo-camera';

import styles from './styles';

export default ({ captures = [] }) => {
    const [isVisible, setIsVisible] = useState(false);

    const takePicture = async () => {
        const photoData = await Camera.takePictureAsync();
        if (!photoData.cancelled) {
            uploadImage(photoData.uri, imageName)
                .then(() => {
                    Alert.alert("Success");
                })
                .catch((error) => {
                    Alert.alert('Error:', error.message);
                });
        }
    }

    const uploadImage = async (uri, imageName) => {
        const response = await fetch(uri);
        const blob = await response.blob();
        var ref = storage().ref().child("images/" + imageName);
        return ref.put(blob)
    }

    return (
        <Modal
            transparent={true}
            visible={isVisible}
        // style={[styles.bottomToolbar, styles.galleryContainer]} 
        >
            <View style={{ backgroundColor: "#000000aa", flex: 1 }}>
                {captures.map(({ uri }) => (
                    <View style={styles.galleryImageContainer} key={uri}>
                        <Image source={{ uri }} style={styles.galleryImage} />
                    </View>
                ))}
            </View>
            <TouchableOpacity
                style={{
                    justifyContent: 'center',
                    alignItems: 'center',
                    marginTop: 20,
                    top: -20
                }}
                onPress={() => setIsVisible(false)}
            >
                <Ionicons
                    name="md-reverse-camera"
                    color="white"
                    size={40}
                />
                <Text style={{ color: 'white' }}>Discard</Text>
            </TouchableOpacity>
            <Button
                title='Save'
                onPress={takePicture}
            />
        </Modal>
    );
};

【讨论】:

  • 您好,感谢您的回复...我已经删除了字符串“this”和“this.state”,但是当我点击图标/放弃按钮时,我仍然无法关闭模式。我设置了 visible={true},然后 onPress={() => {displayModal(false)}} 我该如何纠正它
  • 在“Ionicons”关闭图标上设置 onPress 如下:onPress={() => setIsVisible(false)}
  • 我试过了,但是没用……它没有关闭模态
  • 看不到整个代码就无法提供帮助
  • 谢谢@D10S 我已经把项目上传到了github ---这是文件的链接---github.com/wiredmatrix/vocvedo/blob/master/src/…整个项目的链接---github.com/wiredmatrix/vocvedo请在这里回复你的帮助。 ..非常感谢
猜你喜欢
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
  • 2017-12-20
相关资源
最近更新 更多