{"id":111141,"date":"2021-08-27T07:00:00","date_gmt":"2021-08-27T04:00:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=111141"},"modified":"2021-08-20T12:45:08","modified_gmt":"2021-08-20T09:45:08","slug":"how-to-close-a-react-native-modal-with-a-button","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html","title":{"rendered":"How to close a React Native modal with a button"},"content":{"rendered":"<p>I\u2019ve been working with React Native lately, and I\u2019ve come across a few unimplemented features I needed to add to the basic components. One of those components is the React Native <em>Modal <\/em>component that is used to show a view above another one. It doesn\u2019t promise more features than that, but it can be very useful to implement your own popup.<\/p>\n<p>On the other hand, since it\u2019s only a simple modal, it does not include everything you would expect for a popup component like an X to close it, or closing the modal when you tap beside it. I\u2019ll be walking you through how to add those two features when you\u2019re starting from a <em>Modal <\/em>component.<\/p>\n<p>If you\u2019re in a hurry, you can also find the final result on my GitHub: <a href=\"https:\/\/github.com\/CindyPotvin\/react-native-popup\">https:\/\/github.com\/CindyPotvin\/react-native-popup<\/a><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/08\/Screenshot_20210813-211004-576x1024-1.jpg\"><img decoding=\"async\" width=\"576\" height=\"1024\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/08\/Screenshot_20210813-211004-576x1024-1.jpg\" alt=\"\" class=\"wp-image-111155\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/08\/Screenshot_20210813-211004-576x1024-1.jpg 576w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/08\/Screenshot_20210813-211004-576x1024-1-169x300.jpg 169w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">How to close a modal<\/h2>\n<p>Let\u2019s start with a basic <em>App.js <\/em>that shows a popup. I also added a few styles to have a shadow so the popup is visible, and a generous amount of margin so we can test closing the popup by clicking beside it later.<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">import React, { useState } from 'react';\nimport { StyleSheet, Modal, Text, View } from 'react-native';\n\nexport default function App() {\n  const [modalVisible, setModalVisible] = useState(true);\n\n  return (\n    &lt;View style={styles.container}&gt;\n      &lt;Text&gt;Open up App.js to start working on your app!&lt;\/text&gt;\n      &lt;Modal visible={modalVisible}&gt;\n        &lt;View style={styles.modal}&gt;\n          &lt;Text&gt;\n            Popup content.\n          &lt;\/Text&gt;\n        &lt;\/View&gt;\n      &lt;\/Modal&gt;\n    &lt;\/View&gt;\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    backgroundColor: \"white\",\n    alignItems: \"center\",\n    justifyContent: \"center\",\n  },\n  modal: {\n    flex: 1,\n    margin: 50,\n    padding: 5,\n    backgroundColor: \"white\",\n    shadowColor: \"black\",\n    shadowOffset: {\n      width: 0,\n      height: 2,\n    },\n    shadowOpacity: 0.25,\n    shadowRadius: 4,\n    elevation: 5,\n  },\n});<\/pre>\n<p>The <em>Modal <\/em>is displayed according to its <em>visible<\/em> prop. Right now, the value in the state is always true, so the popup will always be visible.<\/p>\n<p>Even without a button or another way to change the state, the <em>Modal <\/em>can be closed on Android with the back button (the menu button on Apple TV should have the same effect according to the documentation, but I don\u2019t have one to test).<\/p>\n<p>Pressing the button calls the function specified in the <em>onRequestClose<\/em> prop of the modal, so let\u2019s change the state to hide the popup when it is pressed, which will show the main screen of the application :<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;Modal\n  visible={modalVisible}\n  onRequestClose={() =&gt; setModalVisible(false)}&gt;<\/pre>\n<h2 class=\"wp-block-heading\">How to add close button to a React Native modal<\/h2>\n<p>First, we have to add the button itself so we can then use it to close the popup. In that case, I wanted to add a small X in the top right corner of the popup, but it could be anywhere else.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Given how the positioning work, there are two options for this:<\/p>\n<ul class=\"wp-block-list\">\n<li>Positioning absolutely the button in the top right corner. You can then do anything you want for the rest of the content, but then you run the risk of having the content overlapping the X since it\u2019s out of the normal layout flow. If you want to use the space beside and under the button at the same time, it\u2019s pretty much impossible, which leads us to the second option.<\/li>\n<li>Positioning the button with flexbox, leaving space to the left of the button for a header. You can then fill in the header and the content at the bottom separately. If you\u2019re doing something that\u2019s meant to be a popup, having a title is also a pretty standard feature.<\/li>\n<\/ul>\n<p>Here is what the component now looks like with an X added:<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">import React, { useState } from 'react';\nimport { StyleSheet, Modal, Text, View, TouchableOpacity } from 'react-native';\n\nexport default function App() {\n  const [modalVisible, setModalVisible] = useState(true);\n\n  return (\n    &lt;View style={styles.container}&gt;\n      &lt;Text&gt;Open up App.js to start working on your app!&lt;\/Text&gt;\n      &lt;Modal visible={modalVisible} onRequestClose={() =&gt; setModalVisible(false)}&gt;\n        &lt;View style={styles.modal}&gt;\n          &lt;View style={styles.modalHeader}&gt;\n            &lt;View style={styles.modalHeaderContent}&gt;&lt;Text&gt;Other header content&lt;\/Text&gt;&lt;\/View&gt;\n            &lt;TouchableOpacity&gt;&lt;Text style={styles.modalHeaderCloseText}&gt;X&lt;\/Text&gt;&lt;\/TouchableOpacity&gt;\n          &lt;\/View&gt;\n          &lt;View style={styles.modalContent}&gt;\n            &lt;Text&gt;\n              Popup content.\n            &lt;\/Text&gt;\n          &lt;\/View&gt;\n        &lt;\/View&gt;\n      &lt;\/Modal&gt;\n    &lt;\/View&gt;\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    backgroundColor: \"white\",\n    alignItems: \"center\",\n    justifyContent: \"center\",\n  },\n  modal: {\n    flex: 1,\n    margin: 50,\n    padding: 5,\n    backgroundColor: \"white\",\n    shadowColor: \"black\",\n    shadowOffset: {\n      width: 0,\n      height: 2,\n    },\n    shadowOpacity: 0.25,\n    shadowRadius: 4,\n    elevation: 5,\n  },\n  \/* The content of the modal takes all the vertical space not used by the header. *\/\n  modalContent: {\n    flex: 1\n  },\n  modalHeader: {\n    flexDirection: \"row\",\n  },\n  \/* The header takes up all the vertical space not used by the close button. *\/\n  modalHeaderContent: {\n    flexGrow: 1,\n  },\n  modalHeaderCloseText: {\n    textAlign: \"center\",\n    paddingLeft: 5,\n    paddingRight: 5\n  }\n});<\/pre>\n<p>The header is a flex container that\u2019s displayed as a row, with <code>flexGrow:1<\/code> to indicates that it should take up all the remaining space.<\/p>\n<p>The rest of the content in the popup is a <code>flex:1<\/code> item so it takes up all the remaining height.<\/p>\n<p>The only thing remaining at this point is to wire up the button so it does close the popup, in the same way we set it in on the <em>onRequestClose <\/em>event earlier:<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;TouchableOpacity onPress={() =&gt; setModalVisible(false)}&gt;\n  &lt;Text style={styles.modalHeaderCloseText}&gt;X&lt;\/Text&gt;\n&lt;\/TouchableOpacity&gt;<\/pre>\n<h2 class=\"wp-block-heading\">How to close the modal by clicking outside<\/h2>\n<p>To also close the modal by tapping outside, we need an additional component to catch those taps.<\/p>\n<p>On the other hand, we don\u2019t want this component to catch taps meant for the child component: clicking the popup itself should not close it. By checking <code>event.target&nbsp;==&nbsp;event.currentTarget<\/code>, we validate that the item selected is the component itself and not one of its children.<\/p>\n<p>I used a <em>Pressable<\/em> component because I didn\u2019t want the \u201cdimming\u201d effect on tap when touching outside the popup that comes with the <em>TouchableOpacity<\/em>. This new <em>Pressable <\/em>component wraps all the components we previously defined in the modal.<\/p>\n<p>Here is the completed popup, with a few extra borders to show where are the header and popup content :<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">import React, { useState } from 'react';\nimport { StyleSheet, Modal, Text, View, Pressable, TouchableOpacity } from 'react-native';\n\nexport default function App() {\n  const [modalVisible, setModalVisible] = useState(true);\n  return (\n    &lt;View style={styles.container}&gt;\n      &lt;Text&gt;Open up App.js to start working on your app!&lt;\/Text&gt;\n      &lt;Modal\n        visible={modalVisible}\n        onRequestClose={() =&gt; setModalVisible(false)}&gt;\n        &lt;Pressable style={styles.outsideModal}\n          onPress={(event) =&gt; { if (event.target == event.currentTarget) { \n            setModalVisible(false); } }} &gt;\n          &lt;View style={styles.modal}&gt;\n            &lt;View style={styles.modalHeader}&gt;\n              &lt;View style={styles.modalHeaderContent}&gt;&lt;Text&gt;Other header content&lt;\/Text&gt;&lt;\/View&gt;\n              &lt;TouchableOpacity onPress={() =&gt; setModalVisible(false)}&gt;\n                &lt;Text style={styles.modalHeaderCloseText}&gt;X&lt;\/Text&gt;\n              &lt;\/TouchableOpacity&gt;\n            &lt;\/View&gt;\n            &lt;View style={styles.modalContent}&gt;\n              &lt;Text&gt;\n                Popup content.\n              &lt;\/Text&gt;\n            &lt;\/View&gt;\n          &lt;\/View&gt;\n        &lt;\/Pressable&gt;\n      &lt;\/Modal&gt;\n    &lt;\/View&gt;\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    backgroundColor: \"white\",\n    alignItems: \"center\",\n    justifyContent: \"center\",\n  },\n  modal: {\n    flex: 1,\n    margin: 50,\n    padding: 5,\n    backgroundColor: \"white\",\n    shadowColor: \"black\",\n    shadowOffset: {\n      width: 0,\n      height: 2,\n    },\n    shadowOpacity: 0.25,\n    shadowRadius: 4,\n    elevation: 5,\n  },\n  \/* The content of the modal takes all the vertical space not used by the header. *\/\n  modalContent: {\n    flex: 1,\n    borderWidth: 1,\n    borderColor: \"black\"\n  },\n  modalHeader: {\n    flexDirection: \"row\",\n    borderWidth: 1,\n    borderColor: \"black\"\n  },\n  \/* The header takes up all the vertical space not used by the close button. *\/\n  modalHeaderContent: {\n    flexGrow: 1,\n  },\n  modalHeaderCloseText: {\n    textAlign: \"center\",\n    paddingLeft: 5,\n    paddingRight: 5\n  },\n  outsideModal: {\n    backgroundColor: \"rgba(1, 1, 1, 0.2)\",\n    flex: 1,\n  }\n});<\/pre>\n<p>Please note that there is a small limitation to this: the background color for a <em>Pressable <\/em>or a <em>TouchableOpacity <\/em>cannot be set to a transparent or a semi-transparent value, so the content under the popup will no longer be visible.<\/p>\n<p>A next step to make this better would be to package the <em>Modal <\/em>components and all its content into a new component that can be reused in your application so you can insert any header or content in the popup, but this is outside the scope of the current article.<\/p>\n<p>If you want to execute the final version yourself and try it out, you can see it on GitHub here: <a href=\"https:\/\/github.com\/CindyPotvin\/react-native-popup\">https:\/\/github.com\/CindyPotvin\/react-native-popup<\/a> and as an Expo Snack here: <a href=\"https:\/\/snack.expo.dev\/@cindyptn\/react-native-popup-with-x-button\">https:\/\/snack.expo.dev\/@cindyptn\/react-native-popup-with-x-button<\/a><\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>Published on Java Code Geeks with permission by Cindy Potvin, partner at our <a href=\"\/\/www.javacodegeeks.com\/join-us\/jcg\/\" target=\"_blank\" rel=\"noopener\">JCG program<\/a>. See the original article here: <a href=\"https:\/\/blog.cindypotvin.com\/how-to-close-a-react-native-modal-with-a-button\/\" target=\"_blank\" rel=\"noopener\">How to close a React Native modal with a button<\/a><\/p>\n<p>Opinions expressed by Java Code Geeks contributors are their own.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve been working with React Native lately, and I\u2019ve come across a few unimplemented features I needed to add to the basic components. One of those components is the React Native Modal component that is used to show a view above another one. It doesn\u2019t promise more features than that, but it can be very &hellip;<\/p>\n","protected":false},"author":574,"featured_media":92051,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1903],"tags":[1692,803],"class_list":["post-111141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-js","tag-android","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to close a React Native modal with a button - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Interested to learn about React Native? Check our article explaining how to close a React Native modal with a button.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to close a React Native modal with a button - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Interested to learn about React Native? Check our article explaining how to close a React Native modal with a button.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-27T04:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Cindy Potvin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CindyPtn\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Cindy Potvin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html\"},\"author\":{\"name\":\"Cindy Potvin\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/98bd8ca1816fefd05d14d0ca39021e03\"},\"headline\":\"How to close a React Native modal with a button\",\"datePublished\":\"2021-08-27T04:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html\"},\"wordCount\":878,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/react-logo.jpg\",\"keywords\":[\"Android\",\"JavaScript\"],\"articleSection\":[\"React.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html\",\"name\":\"How to close a React Native modal with a button - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/react-logo.jpg\",\"datePublished\":\"2021-08-27T04:00:00+00:00\",\"description\":\"Interested to learn about React Native? Check our article explaining how to close a React Native modal with a button.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/react-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/react-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/08\\\/how-to-close-a-react-native-modal-with-a-button.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Web Development\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"React.js\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\\\/javascript\\\/react-js\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to close a React Native modal with a button\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/98bd8ca1816fefd05d14d0ca39021e03\",\"name\":\"Cindy Potvin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/957776d6732b26df4f63c29c8a3cb127ef09eb38b9f5befec1840ee3a1abed32?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/957776d6732b26df4f63c29c8a3cb127ef09eb38b9f5befec1840ee3a1abed32?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/957776d6732b26df4f63c29c8a3cb127ef09eb38b9f5befec1840ee3a1abed32?s=96&d=mm&r=g\",\"caption\":\"Cindy Potvin\"},\"description\":\"Cindy is a programmer with over 5 years of experience developing web applications. She also works on Android applications and share her knowledge via her blog and on Twitter.\",\"sameAs\":[\"http:\\\/\\\/blog.cindypotvin.com\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/CindyPtn\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/cindy-potvin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to close a React Native modal with a button - Java Code Geeks","description":"Interested to learn about React Native? Check our article explaining how to close a React Native modal with a button.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html","og_locale":"en_US","og_type":"article","og_title":"How to close a React Native modal with a button - Java Code Geeks","og_description":"Interested to learn about React Native? Check our article explaining how to close a React Native modal with a button.","og_url":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2021-08-27T04:00:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","type":"image\/jpeg"}],"author":"Cindy Potvin","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CindyPtn","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Cindy Potvin","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html"},"author":{"name":"Cindy Potvin","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/98bd8ca1816fefd05d14d0ca39021e03"},"headline":"How to close a React Native modal with a button","datePublished":"2021-08-27T04:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html"},"wordCount":878,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","keywords":["Android","JavaScript"],"articleSection":["React.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html","url":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html","name":"How to close a React Native modal with a button - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","datePublished":"2021-08-27T04:00:00+00:00","description":"Interested to learn about React Native? Check our article explaining how to close a React Native modal with a button.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2021\/08\/how-to-close-a-react-native-modal-with-a-button.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Web Development","item":"https:\/\/www.javacodegeeks.com\/category\/web-development"},{"@type":"ListItem","position":3,"name":"JavaScript","item":"https:\/\/www.javacodegeeks.com\/category\/web-development\/javascript"},{"@type":"ListItem","position":4,"name":"React.js","item":"https:\/\/www.javacodegeeks.com\/category\/web-development\/javascript\/react-js"},{"@type":"ListItem","position":5,"name":"How to close a React Native modal with a button"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/98bd8ca1816fefd05d14d0ca39021e03","name":"Cindy Potvin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/957776d6732b26df4f63c29c8a3cb127ef09eb38b9f5befec1840ee3a1abed32?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/957776d6732b26df4f63c29c8a3cb127ef09eb38b9f5befec1840ee3a1abed32?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/957776d6732b26df4f63c29c8a3cb127ef09eb38b9f5befec1840ee3a1abed32?s=96&d=mm&r=g","caption":"Cindy Potvin"},"description":"Cindy is a programmer with over 5 years of experience developing web applications. She also works on Android applications and share her knowledge via her blog and on Twitter.","sameAs":["http:\/\/blog.cindypotvin.com\/","https:\/\/x.com\/https:\/\/twitter.com\/CindyPtn"],"url":"https:\/\/www.javacodegeeks.com\/author\/cindy-potvin"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/111141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/574"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=111141"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/111141\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/92051"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=111141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=111141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=111141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}