Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

reactjs - Modal not opening when i go back from another screen

Modal not opening when i go back from another screen. I'm navigate to my second screen and press "back". Now, when i try to open modal its not work.

Main

....
const nav_to_lights =() =>{
        navigation.navigate('Lights');
    }
....
return(
....
        <Modal visible={visible} onBackdropPress={() => Change_visible()} backdropStyle={styles.backdrop}>
        <Card disabled={true}>

          <Button
          style={styles.iconcl}
          appearance='ghost'
          accessoryLeft={CloseIcon}
          onPress={() => Change_visible()}
          />
          <Button style={styles.Buttonset} onPress={chtg}>
          {mtheme} 
            </Button>
        </Card>
      </Modal>
      
...

<Button onPress={nav_to_lights}  style={styles.Button}>
            Lights ??????
            </Button>
....
)

And when i press back button from react navigate header modal not opening.But state of 'visible' changing. I think it's not work because page not render when i go back.

question from:https://stackoverflow.com/questions/65904953/modal-not-opening-when-i-go-back-from-another-screen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Usually you can solve these issues by force rendering the element on the event you want, one way to accomplish this is to put a key on the element and change it when you need a force render, like so:

 <Modal key={myKey} visible={visible} onBackdropPress={() => Change_visible()} backdropStyle={styles.backdrop}>

onMount (or whatever you use)
myKey++

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...