Button is Not visible under Image – React Native App

I am native Android developer, pretty new to React Native. I have 3 issue with my code. I was looking over Internet but can’t find accurate answers. Any help would be Thankful.

  1. Close Button Not visible in the view:
    • In DialogAds.js file, I have a modal, in that I have a Image which fill whole modal, and 1 image inside touchable opacity which is close button image. Here the issue is, it doesn’t show close button, close button seems to be below the whole filled image, but i want it to be in top right above the filled image.
  2. Tigger Button only works one time:
    • In HomeScreen.js file, I have a Button, when I click that button it should change the state of is visible to true, it works fine for the first time. But when I back pressed, when Modal was visible and when I tried to triggered modal 2nd time it doesn’t do any action.
  3. How can I change the value of isVisible to false when I Back Pressed when modal was visible?*

HomeScreen:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// screens/HomeScreen.js
import React, { useRef, useState } from "react";
import { View, Text, Button,StyleSheet } from 'react-native';
import PopupDialogAd from '../utils/DialogAds'
const HomeScreen = ({ navigation }) => {
const [isDialogVisible, setIsDialogVisible] = useState(false);
return (
<View style={styles.container}>
<Button
title="Trigger"
onPress={() => setIsDialogVisible(true)}
/>
<PopupDialogAd
visible={isDialogVisible}
url={'https://picsum.photos/2000/3000'} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
})
export default HomeScreen;
</code>
<code>// screens/HomeScreen.js import React, { useRef, useState } from "react"; import { View, Text, Button,StyleSheet } from 'react-native'; import PopupDialogAd from '../utils/DialogAds' const HomeScreen = ({ navigation }) => { const [isDialogVisible, setIsDialogVisible] = useState(false); return ( <View style={styles.container}> <Button title="Trigger" onPress={() => setIsDialogVisible(true)} /> <PopupDialogAd visible={isDialogVisible} url={'https://picsum.photos/2000/3000'} /> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, }) export default HomeScreen; </code>
// screens/HomeScreen.js
import React, { useRef, useState } from "react";
import { View, Text, Button,StyleSheet } from 'react-native';
import PopupDialogAd from '../utils/DialogAds'

const HomeScreen = ({ navigation }) => {
    const [isDialogVisible, setIsDialogVisible] = useState(false);

    return (
        <View style={styles.container}>
            <Button
                title="Trigger"
                onPress={() => setIsDialogVisible(true)}
            />
            <PopupDialogAd 
            visible={isDialogVisible}
            url={'https://picsum.photos/2000/3000'} />
        </View>
    );
};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
})

export default HomeScreen;

DialogAds:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import React, { useState, useEffect } from "react";
import { Modal, View, Image, Text, Button, StyleSheet, ActivityIndicator } from "react-native";
import { TouchableOpacity } from "react-native-gesture-handler";
import axios from "axios";
const PopupDialogAd = ({ visible, onClose, url }) => {
const [isVisible, setIsVisible] = useState(visible);
const [loading, setLoading] = useState(false);
useEffect(() => {
setIsVisible(visible);
}, [visible]);
const fetchImage = () => {
setLoading(true);
axios.get(url)
.then(response => {
setImageUrl("");
})
.catch(error => {
console.error("Error fetching image: ", error);
})
.finally(() => {
setLoading(false);
});
};
const showDialogAd = () => {
fetchImage();
};
return(
<View>
<Modal
visible={isVisible}
transparent={true}
onRequestClose={() => setIsVisible(false)}>
<View style={styles.modalContainer}>
<TouchableOpacity style={styles.closeButton} onPress={() => setIsVisible(false)}>
<Image source={require('../assets/ic_close.png')} style={styles.closeIcon}/>
</TouchableOpacity>
<Image style={styles.adImage} source={{uri: url}} />
</View>
</Modal>
</View>
)
}
const styles = StyleSheet.create({
modalContainer: {
flex: 1,
alignSelf: 'center',
backgroundColor: 'rgba(0, 0, 0, 1)',
justifyContent: 'center',
width: '80%',
marginTop: '40%',
marginBottom: '40%',
elevation: 20,
},
closeButton: {
position: 'absolute',
top: 10,
right: 10,
backgroundColor: 'rgba(256, 256, 256, 100)',
borderRadius: 50,
zIndex: 1,
elevation: 10,
},
closeIcon: {
width: 25,
height: 25,
resizeMode: 'contain',
zIndex: 2,
},
adImage: {
zIndex: 0,
elevation: 0,
width: '100%',
height: '100%'
}
});
export default PopupDialogAd;
</code>
<code>import React, { useState, useEffect } from "react"; import { Modal, View, Image, Text, Button, StyleSheet, ActivityIndicator } from "react-native"; import { TouchableOpacity } from "react-native-gesture-handler"; import axios from "axios"; const PopupDialogAd = ({ visible, onClose, url }) => { const [isVisible, setIsVisible] = useState(visible); const [loading, setLoading] = useState(false); useEffect(() => { setIsVisible(visible); }, [visible]); const fetchImage = () => { setLoading(true); axios.get(url) .then(response => { setImageUrl(""); }) .catch(error => { console.error("Error fetching image: ", error); }) .finally(() => { setLoading(false); }); }; const showDialogAd = () => { fetchImage(); }; return( <View> <Modal visible={isVisible} transparent={true} onRequestClose={() => setIsVisible(false)}> <View style={styles.modalContainer}> <TouchableOpacity style={styles.closeButton} onPress={() => setIsVisible(false)}> <Image source={require('../assets/ic_close.png')} style={styles.closeIcon}/> </TouchableOpacity> <Image style={styles.adImage} source={{uri: url}} /> </View> </Modal> </View> ) } const styles = StyleSheet.create({ modalContainer: { flex: 1, alignSelf: 'center', backgroundColor: 'rgba(0, 0, 0, 1)', justifyContent: 'center', width: '80%', marginTop: '40%', marginBottom: '40%', elevation: 20, }, closeButton: { position: 'absolute', top: 10, right: 10, backgroundColor: 'rgba(256, 256, 256, 100)', borderRadius: 50, zIndex: 1, elevation: 10, }, closeIcon: { width: 25, height: 25, resizeMode: 'contain', zIndex: 2, }, adImage: { zIndex: 0, elevation: 0, width: '100%', height: '100%' } }); export default PopupDialogAd; </code>
import React, { useState, useEffect } from "react";
import { Modal, View, Image, Text, Button, StyleSheet, ActivityIndicator } from "react-native";
import { TouchableOpacity } from "react-native-gesture-handler";
import axios from "axios";

const PopupDialogAd = ({ visible, onClose, url }) => {
    const [isVisible, setIsVisible] = useState(visible);
    const [loading, setLoading] = useState(false);

    useEffect(() => {
        setIsVisible(visible);
    }, [visible]);

    const fetchImage = () => {
        setLoading(true);
        axios.get(url)
            .then(response => {
                setImageUrl("");
            })
            .catch(error => {
                console.error("Error fetching image: ", error);
            })
            .finally(() => { 
                setLoading(false);
            });
    };

    const showDialogAd = () => {
        fetchImage();
    };

    return(
        <View>
            <Modal
                visible={isVisible}
                transparent={true}
                onRequestClose={() => setIsVisible(false)}>
                <View style={styles.modalContainer}>
                    <TouchableOpacity style={styles.closeButton} onPress={() => setIsVisible(false)}>
                        <Image source={require('../assets/ic_close.png')} style={styles.closeIcon}/>
                    </TouchableOpacity>
                    <Image style={styles.adImage} source={{uri: url}} />
                </View>
            </Modal>
        </View>
    )
}

const styles = StyleSheet.create({
    modalContainer: {
      flex: 1,
      alignSelf: 'center',
      backgroundColor: 'rgba(0, 0, 0, 1)',
      justifyContent: 'center',
      width: '80%',
      marginTop: '40%',
      marginBottom: '40%',
      elevation: 20,
    },
    closeButton: {
        position: 'absolute',
        top: 10,
        right: 10,
        backgroundColor: 'rgba(256, 256, 256, 100)',
        borderRadius: 50,
        zIndex: 1,
        elevation: 10,
      },
    closeIcon: {
        width: 25,
        height: 25,
        resizeMode: 'contain',
        zIndex: 2,
      },
    adImage: {
        zIndex: 0,
        elevation: 0,
        width: '100%', 
        height: '100%'
    }  
});

export default PopupDialogAd;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật