modalfy
Function that exposes Modalfy's API outside of React's context.
Notes:
- You can't pass a
modalName
tocloseModal()
to close a modal that's not the currently displayed one. This is due to a current API limitation especially in the case where you'd have several modals with the same name opened, as Modalfy can't safely determine which one you wanted to close.
TypeScript
const modalfy = <
P extends ModalfyParamsm,
M extends keyof P
>(): UsableModalProp<P> => {
const context: UsableModalProp<P> = React.useContext(ModalContext)
return {
closeAllModals: ModalState.closeAllModals,
closeModal: () => ModalState.closeModal(),
closeModals: (modalName: M) => ModalState.closeModals(modalName),
currentModal: ModalState.getState<P>()?.currentModal,
openModal: (modalName: M, params?: P[M]) =>
ModalState.openModal(modalName, params, true),
}
}

react-native-modalfy/ModalState.ts at main · colorfy-software/react-native-modalfy
GitHub
Types have been simplified for the sake of clarity. Refer to the exact definitions here.
If you're using TypeScript and have your params types, you can get some nice autocomplete by utilising
modalfy()
like this:import { ModalStackParamsList } from 'App'
// ...
const { openModal } = modalfy<ModalStackParamsList>()
Last modified 2yr ago