简体   繁体   English

如何在React Native中关闭模式?

[英]How to close a modal in react native?

I am unable to close a modal. 我无法关闭模式。 I am displaying few images inside it, and onPress of the "X(close)" icon, want to close the modal. 我在其中显示了少量图像,并且“ X(close)”图标的onPress想关闭模式。 I have tried setting the state of modalvisible to false, by default which is set to true. 我尝试将modalvisible的状态设置为false,默认情况下将其设置为true。 But on press of icon the modal doesn't gets closed. 但是在按下图标时,模态不会关闭。 Any solution would be of great help. 任何解决方案都会有很大帮助。

 export default class imagenav extends Component{
  constructor(props){
    super(props)
    state = {
      modalVisible: false,
  }
}
openmodal(){
  this.setState(modalVisible: true)
}

render() {
  return (
    <Container>
      <Modal  onRequestClose={() => {}}> 
      <GallerySwiper
          style={{ flex: 1, backgroundColor: "black" }}
          images={[
            {source: {uri: "https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png",

              dimensions: {width: 1080, height: 1920}}
          },
          ]}
      />
      <Header
            style={{
              backgroundColor: 'black',
              borderBottomWidth: 0,
            }}
          > 
      <Right>
              <Icon
                name='close'
                color='white'
                onPress={() => {
                   this.setState({
                    modalVisible: false,
                  })  

                  console.log("getting closed");

                }} 
              />
              </Right>
              </Header>

      </Modal>
      </Container>
  );
}
}

It is enough to put a state for it in visible as bellow: 在下面将其置于一个可见状态就足够了:

<Modal onRequestClose={()=> null} visible={this.state.active} transparent={true}>

       /////your Views and things to show in modal


</Modal>

in your state you have to make it as blew: 在您的状态下,您必须使它自爆:

    constructor(props) {
        super();
        this.state =  {
          active:false,
        }

      }

And then you have to toggle it in an onPress for example: 然后,您必须在onPress中进行切换,例如:

   onPress=()={
       this.setState({active:true})
    }

So totally in your project you will have: 因此,完全在您的项目中您将拥有:

 export default class imagenav extends Component{
      constructor(props){
        super(props)
        state = {
          modalVisible: false,
      }
    }
    openmodal(){
      this.setState({modalVisible: true})
    }
    render() {
      return (
        <Container>
          <Modal  visible={this.state.modalVisible} onRequestClose={() => {}}> 
          <View style={{flex:1}}>
          <GallerySwiper
              style={{ flex: 1, backgroundColor: "black" }}
              images={[
                {source: {uri: "https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png",

                  dimensions: {width: 1080, height: 1920}}
              },
              ]}
          />
          <Header
                style={{
                  backgroundColor: 'black',
                  borderBottomWidth: 0,
                }}
              > 
          <Right>
                  <Icon
                    name='close'
                    color='white'
                    onPress={() => {
                       this.setState({
                        modalVisible: false,
                      })  

                      console.log("getting closed");

                    }} 
                  />
                  </Right>
                  </Header>
           </View>

          </Modal>
          </Container>
      );
    }
    }

Update: According to your last request there is a way. 更新:根据您的最后一个请求,有一种方法。 You can pass flag to your next screen and in the componentDidMount() of next screen you can check it. 您可以将标志传递到下一个屏幕,并且可以在下一个屏幕的componentDidMount()中进行检查。 if it is true you can show the modal otherwise ignore it. 如果为true,则可以显示模式,否则将其忽略。

I hope I could help. 希望我能帮上忙。 :) :)

You could use an inline if to only render your modal is your state allows it : if仅在状态允许的情况下渲染模态,则可以使用内联:

{this.state.modalVisible && 
    <Modal onRequestClose={() => { }}>
        <GallerySwiper
            style={{ flex: 1, backgroundColor: "black" }}
            images={[
                {
                    source: {
                        uri: "https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png",

                        dimensions: { width: 1080, height: 1920 }
                    }
                },
            ]}
        />
        <Header
            style={{
                backgroundColor: 'black',
                borderBottomWidth: 0,
            }}
        >
            <Right>
                <Icon
                    name='close'
                    color='white'
                    onPress={() => {
                        this.setState({
                            modalVisible: false,
                        })

                        console.log("getting closed");

                    }}
                />
            </Right>
        </Header>
    </Modal>
}

You have a state, but you are not using it 您有一个状态,但是没有使用它

<Modal onRequestClose={()=> this.openmodal(false)} visible={this.state.modalVisible}> should be good to go. <Modal onRequestClose={()=> this.openmodal(false)} visible={this.state.modalVisible}>应该很好。

oh and your openmodal function can be used for opening and closing the modal 哦,您的openmodal函数可用于打开和关闭模式

openmodal(value){
  this.setState({modalVisible: value})
}

<Icon
                    name='close'
                    color='white'
                    onPress={() => {
                      this.openmodal(false)   
                      console.log("getting closed");
                    }} 
                  />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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