Notice
Recent Posts
Recent Comments
08-21 14:21
«   2025/08   »
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
31
Archives
Today
Total
관리 메뉴

YESHTML5

React-Native ActionSheet 본문

React,React-Native

React-Native ActionSheet

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

앱에서 BottomSheet라고 불리는 아래에서 레이어팝업처럼 나오는

react-native-actions-sheet 중에 괜찮은것으로 골라서 hooks로 만들었다. 

TypeScript체크를 조금더 꼼꼼히 해야되어야하나, 사용중인 TS파일 하나 올려본다.

https://www.npmjs.com/package/react-native-actions-sheet

 

react-native-actions-sheet

A Cross Platform(Android & iOS) ActionSheet with a robust and flexible api, native performance and zero dependency code for react native. Create anything you want inside ActionSheet.. Latest version: 0.6.1, last published: 2 months ago. Start using react-n

www.npmjs.com

/**
 * @name useActionSheet
 * @description useActionSheet
 */
import React, {createRef, useEffect} from 'react'
import {View, StyleSheet, ViewStyle} from 'react-native'
import ActionSheet, {ActionSheetProps} from 'react-native-actions-sheet'

type Props = {
  style?: ViewStyle
  onClose?: () => void
  visible?: boolean
  setVisible?: any
  component?: any
  asProps?: ActionSheetProps
}
// Hook
export const useActionSheet = ({style, visible, onClose, setVisible, component, asProps}: Props) => {
  // hooks
  const actionSheetRef = createRef() as any
  actionSheetRef.current?.hide()
  // *-------------------------------------------------------------------
  useEffect(() => {
    actionSheetRef.current?.setModalVisible(visible)
  }, [visible])
  // *----- 액션시트컨텐츠
  const actionSheet = () => {
    return (
      <ActionSheet
        id="ActionSheet"
        {...asProps}
        ref={actionSheetRef}
        onClose={() => {
          onClose && onClose()
          setVisible && setVisible(false)
        }}>
        <View style={[styles.actionSheet, style]}>{component()}</View>
      </ActionSheet>
    )
  }
  return {actionSheet, visible}
}
// styles
const styles = StyleSheet.create({
  /**@actionSheet */
  actionSheet: {overflow: 'hidden', borderRadius: 10, minHeight: 200},
  /**@text */
})

/********************************************************
[사용법]
  import { useActionSheet } from '@app/lib/hooks'
  const [visible, setVisible] = useState<boolean>(false)
  const {actionSheet} = useActionSheet({
    visible: actionSheetStore.visible,
    component: () => <Txt>actionSheet컨텐츠</Txt>,
  })
return(
    {!!actionSheetStore && actionSheet()}
)
[ 다른사용법 contextAPI+onClose]
  const { actionSheet } = useActionSheet({
    'visible': action_sheet_visible, component: () => <ActionSheet />,
    onClose: () => { dispatch({ type: 'VISIBLE_ACTION_SHEET', 'action_sheet_visible': false }) }
  })

*********************************************************/
반응형
Comments