Notice
Recent Posts
Recent Comments
04-30 08:00
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

YESHTML5

useModal hooks React-Native Modal 방법 본문

React,React-Native

useModal hooks React-Native Modal 방법

슬/도/아/밤/ 2022. 5. 19. 20:03
반응형

앱에서 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 >
      )
    }
  })
*********************************************************/
반응형
Comments