react native에서 modal을 띄워놓았을 때 모달 이외의 배경을 클릭해서 modal 창을 닫고 싶으신 상황이 있으실 겁니다.
이런 상황을 저는 다음과 같이 해결했습니다.
<Modal
visible={isVisible}
onRequestClose={() => setIsVisible(false)}
transparent={true}
>
<Pressable style={{
flex:1,
backgroundColor:'transparent',
}}
onPress={()=>setIsVisible(false)}
/>
{/* 여기에 띄우고 싶으신 창 코드를 입력하시면 됩니다. */}
</Modal>
이 때 창 코드를 입력하는 부분에 position:absolute 속성을 줘야 합니다.
Pressable 태그가 배경을 담당하는 부분인데 Pressable이 전체 화면을 덮도록 하기 위함입니다.
아래는 스택오버플러우 질문에 제가 답변을 단 부분입니다.
https://stackoverflow.com/a/68570195/15458307
Close react native modal by clicking on overlay?
Is it possible to close react native modal by clicking on overlay when transparent option is true? Documentation doesn't provide anything about it. Is it possible?
stackoverflow.com
'모바일 앱 > React Native' 카테고리의 다른 글
[React Native] __DEV__ (0) | 2021.08.12 |
---|---|
[React Native] React Native Webview 줌(zoom) 비활성화하기 (0) | 2021.07.29 |
[React Native] WebView <img> click event 처리 (1) | 2021.07.26 |
[React Native] WebView 강제 종료되는 버그 (0) | 2021.07.22 |
[React Native] iOS TextInput autoScroll, autoFocus 되지 않는 이슈 (+TextInput이 키보드에 가려지는 이슈) (0) | 2021.07.13 |