Notice
Recent Posts
Recent Comments
04-30 08:00
YESHTML5
useModal hooks React-Native Modal 방법 본문
반응형
앱에서 Modal 레이어 팝업띄울때, 여러가지 방법이 있다.
우선 React-Native에서 제공하는 Modal 이 있을것이다.
모듈로는
https://www.npmjs.com/package/react-native-modal
react-native-modal
An enhanced React Native modal. Latest version: 13.0.1, last published: 3 months ago. Start using react-native-modal in your project by running `npm i react-native-modal`. There are 342 other projects in the npm registry using react-native-modal.
www.npmjs.com
이것저것 진행하던중
react-native-paper 에서 {Modal} 이 괜찮은것 같아, hooks로 만들었다.
/**
* @name useModal
* @description modal팝업
*/
import React from 'react'
import {View, StyleSheet, ViewStyle} from 'react-native'
import {Modal, Portal} from 'react-native-paper'
type Props = {
style?: ViewStyle | ViewStyle[]
center?: boolean
onClose?: () => void
visible: boolean
setVisible: (bool: boolean) => void
component: any
}
// Hook
export const useModal = ({center, style, visible, setVisible, component}: Props): any => {
// onClose
const onClose = () => {
setVisible(false)
}
// *----- 모달컨텐츠
const modal = () => {
return (
<Portal>
<Modal
visible={visible}
style={style}
onDismiss={() => {
onClose()
}}>
<View style={[center && styles.centerStyle]}>{!!component && component()}</View>
</Modal>
</Portal>
)
}
return {modal, visible}
}
// styles
const styles = StyleSheet.create({
/**@container */
centerStyle: {alignItems: 'center'},
/**@text */
})
/********************************************************
[사용법]
const [visible, setVisible] = useState<boolean>(false)
const { modal } = useModal({
center: true,
'visible': visible, 'setVisible': setVisible,
'component': () => {
return (
<View style={{ width: 340, height: 100, backgroundColor: 'white' }}>
<H1>타이틀</H1>
<FillButton style={{ marginBottom: 20 }} label="modal" onPress={() => {
setVisible(false)
}} />
</View >
)
}
})
*********************************************************/
반응형
'React,React-Native' 카테고리의 다른 글
react-native-device-info 앱빌드버젼표시하기 (0) | 2022.05.20 |
---|---|
React-Native ActionSheet (0) | 2022.05.19 |
React-Native 용 Youtube Player (0) | 2022.05.17 |
error Failed to launch the app on simulator, An error was encountered processing the command (domain=com.apple.CoreSimulator.SimError, code=405):Unable to lookup in current state: Shutdown (0) | 2022.05.13 |
Linking 작업에서 React-Native코드 적용예 (0) | 2022.02.10 |
Comments