1

I am showing a view Login.js and in that view on button click, I need to render modal, that I have separated and written in another file named Countries.js.

On Login.js file I have imported Countries.js and on button click, I am doing this:

show_modal = () => {
    <Countries/>
}

but nothing is happening. I am a noob I just started React Native kindly help me.

Countries.js code:

import React, { Component, useState } from "react";
import {
Alert,
Modal,
Text,
TouchableHighlight,
View
} from "react-native";

const Countries = () => {

    console.log('called');
    
    const [modalVisible, setModalVisible] = useState(true);
    
    return (
    
    <View style={styles.centeredView}>
      <Modal
        animationType="slide"
        transparent={true}
        visible={modalVisible}
        onRequestClose={() => {
          Alert.alert("Modal has been closed.");
        }}
      >
        <View style={styles.centeredView}>
          <View style={styles.modalView}>
            <Text style={styles.modalText}>Hello World!</Text>
    
            <TouchableHighlight
              style={{ ...styles.openButton, backgroundColor: "#2196F3" }}
              onPress={() => {
                setModalVisible(!modalVisible);
              }}
            >
              <Text style={styles.textStyle}>Hide Modal</Text>
            </TouchableHighlight>
          </View>
        </View>
      </Modal>
    
      <TouchableHighlight
        style={styles.openButton}
        onPress={() => {
          setModalVisible(true);
        }}
      >
        <Text style={styles.textStyle}>Show Modal</Text>
      </TouchableHighlight>
    </View>)
};
    
export default Countries;
6
  • hi,can you add Countries.js code so we can se why its not working but from first look my guess is since you didnt any props in Countries component it does not know what to do. Commented Apr 8, 2020 at 13:21
  • some imports I didn't mention here. Commented Apr 8, 2020 at 13:36
  • You can passing props for modalVisible=true and use modal component , Commented Apr 8, 2020 at 13:41
  • where i use modal component? Commented Apr 8, 2020 at 13:43
  • You see i already set the state of modal true by default. Waleed Nasir Commented Apr 8, 2020 at 13:43

3 Answers 3

2

Login.js

import React, { Component, useState } from "react";
import {Modal, View, Text, TouchableHighlight} from 'react-native';
import CountryModal from 'path to outsource country modal file';

const login = (props)=>{
const [modalVisible, setModalVisible] = useState(true);

return(

<View>
    <TouchableHighlight
style={styles.openButton}
onPress={() => {
    setModalVisible(true);
}}
>
<Text style={styles.textStyle}>Show Modal</Text>
</TouchableHighlight>

<CountryModal isVisible={modalVisible} setModalVisiblity = {()=>{setModalVisible(preState=> preState = !preState)}}>
</View>
)
}

export default login;

Country Modal file

import React from react;
import {Modal} from 'react-native';

const Country = (props)=>{

return (
<Modal
    animationType="slide"

    transparent={true}
    visible={isVisible}
    onRequestClose={() => {
      Alert.alert("Modal has been closed.");
    }}
  >
    <View style={styles.centeredView}>
      <View style={styles.modalView}>
        <Text style={styles.modalText}>Hello World!</Text>

        <TouchableHighlight
          style={{ ...styles.openButton, backgroundColor: "#2196F3" }}
          onPress={() => {props.setModalVisiblity
          }}
        >
          <Text style={styles.textStyle}>Hide Modal</Text>
        </TouchableHighlight>
      </View>
    </View>
  </Modal>
)
}

Hope you got your answer.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to have this modal directly with other components. Example code

export default function Login() {

  const [modalVisible, setModalVisible] = useState(false);

  return (
    <View>
      <Button title="Toggle Modal" onPress={() => setModalVisible(!modalVisible)}
      // other login page code
      <Countries visible={visible} /> // or any other modal, add direclty to the screen you need to show the modal at
    </View>
  )

}

2 Comments

Sir i didnt set any props for Countries.
You should remove the visible state from the modal to the parent component. Because it's the state of the parent component, that the modal should be shown or not. So your modal would not contain any state, rather shift the state logic to upper component.
0

Change this:

show_modal = ()=> {
  <Countries/>
}

to this:

show_modal = ()=> {
  return  <Countries/>; // add return keyword 
}

the above function will return undefined if return id is not explicitly defined

3 Comments

Can you give a try in your app and then answer?
where are you call this function show_modal? in react native
inside Login.js file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.