【问题标题】:Modal not getting opened on clicking the image of gallery component单击图库组件的图像时未打开模态
【发布时间】:2022-01-25 09:34:13
【问题描述】:

当我单击图库中的图像组件时,我正在尝试打开模型。在这里,我没有在 TouchableOpacity 的 onPress 方法中更新“flagImage”变量,或者当 TouchableOpacity 的 onPress 更改其值时,模态组件没有获得“flagImage”的更新值。我在这里需要帮助。每当我触摸图像组件时,模态都不会打开。它什么都不做:(下面是gallery.component.js的代码:

import React from 'react';
import { View, Image, ScrollView, TouchableOpacity, Modal, Text } from 'react-native';
import styles from './cameraStyles';

export default ({captures=[], flagImage=true}) => (
    <ScrollView 
        horizontal={true}
        style={[styles.bottomToolbar, styles.galleryContainer]} 
    >
        {captures.map(({ uri }) => (
            <View style={styles.galleryImageContainer} key={uri}>
                <TouchableOpacity 
                    onPress={()=> {
                        flagImage = !flagImage;
                        console.log('Touch: "'+flagImage+'"');          
                    }}
                >
                    <Image source={{ uri }} style={styles.galleryImage}/>
                </TouchableOpacity>
                <Modal visible={flagImage} onPress={() => {console.log('Modal: "'+flagImage+'"');flagImage = !flagImage;}} onRequestClose={() => flagImage = ! flagImage}>
                      <View>
                            <Image source={{ uri }} style={styles.galleryImageLarge}/> 
                      </View>
                </Modal>
            </View>
        ))}
    </ScrollView>
);    

【问题讨论】:

    标签: javascript react-native expo


    【解决方案1】:

    您可以使用 useState 挂钩将道具转换为可以更新的状态。

    import React from 'react';
    import { View, Image, ScrollView, TouchableOpacity, Modal, Text } from 'react-native';
    import styles from './cameraStyles';
    
    export default ({captures=[], lflagImage=false}) => {
        const [flagImage, setflagImage] = React.useState(lflagImage);
        return (
        
            <ScrollView 
                horizontal={true}
                style={[styles.bottomToolbar, styles.galleryContainer]} 
            >
                {captures.map(({ uri }) => (
                    <View style={flagImage?styles.galleryImageContainer:styles.galleryImageContainerView} key={uri}>
                        <TouchableOpacity 
                            onPress={()=> {
                                setflagImage(prevData => !prevData)
                                console.log('Touch: "'+flagImage+'"');          
                            }}
                        >
                            <Image source={{ uri }} style={flagImage?styles.galleryImage:styles.galleryImageLarge}/>
                        </TouchableOpacity>
                        <Modal visible={flagImage} onRequestClose={() => setflagImage(prevData => !prevData)}>
                            <View>
                                    <Image source={{ uri }} style={styles.galleryImageLarge}/>
                            </View>
                        </Modal>
                    </View>
                ))}
            </ScrollView>
            )
        }
    

    【讨论】:

    • 收到以下错误:SyntaxError: E:\Mobile App Development\Advanced Experimentation\TrendingCam\src\gallery.component.js: Unexpected token (6:4) 4 | 5 |导出默认值 ({captures=[], flagImage=false}) => ( > 6 | const [flagImage, setflagImage] = React.useState(flagImage); | ^ 7 |
    • 看看道具我改了道具的名字。
    • 使用与您在上面发布的相同代码仍然得到相同的错误:SyntaxError: E:\Mobile App Development\Advanced Experimentation\TrendingCam\src\gallery.component.js: Unexpected token (6:4 ) 4 | 5 |导出默认 ({captures=[], lflagImage=false}) => ( > 6 | const [flagImage, setflagImage] = React.useState(lflagImage); | ^ 7 |
    • 我在 useState 钩子在 return 语句中之前更新了代码。这不应该发生 useState hook 必须在 return 语句之外。
    • 是的,有效。我错过了这部分,我跳过了 return 语句,因此我无法在这个组件中使用状态。非常感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多