简体   繁体   中英

REACT NATIVE - Trigger Modal on App Screen using a Custom Component

I am trying to trigger on the App Screen a modal using a custom button component.

I created a reusable button named SelectBoxLong.js

在此处输入图片说明

      <TouchableOpacity style={styles.containerLong} onPress={onPress}>
        <Text style={styles.text}>{text}</Text>
        <MaterialCommunityIcons name="menu-down" size={10} />
      </TouchableOpacity>

Also, I created a modal component named FilterModal.js which includes the useState that triggers the modal: 在此处输入图片说明

const FilterModal = ({ text, onPress }) => {
  const [modalVisible, setModalVisible] = React.useState(false);

on the App Screen , I included the component and the FilterModal But I can't manage to make a connection between this 3.

What I want is that when the user taps on the SelectBoxLong the modal triggers the modal, and the header of the modal gets the title of the button

在此处输入图片说明

I Tried

-Create a function inside Modal that triggers Modal

-Pass as props the useState

App screen:

const [modalVisible, setModalVisible] = React.useState(false);
const [buttonTitle, setButtonTitle] = React.useState(‘’);

onPress = () => {
  setModalVisible(true);
}


render(){
    return(
        ……
        <SelectBoxLong.js onPress={onPress} text={text}/>
        <FilterModal modalVisible={modalVisible} onPress={onPress} text={buttonTitle} />
        ….
    );
}

FilterModal.js

const FilterModal = ({ modalVisible , text, onPress }) => {
  const [modalVisible, setModalVisible] = React.useState(modalVisible);


  useEffect(() => {
    setModalVisible(modalVisible);
  }, [modalVisible]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM