Modal prop

Every modal component you added in your modal stack (see createModalStack) receives the modal prop automatically:

  • this.props.modal

    • currentModal: name of the current displayed modal if there's one

    • openModal: open a specific modal

    • closeModal: close a modal

    • closeModals: close every instance of a given modal

    • closeAllModals: close all opened modals

    • addListener: subscribe to updates to modal lifecycle

    • removeAllListeners: remove all modal listeners

    • params: current modal's params

    • getParams: get current's modal params with fallback

Be aware that only components in you modal stack do receive this prop. If you're looking for how to access the modal prop from any component, head to withModal/useModal sections.

API reference

currentModal - Get the currently displayed modal's name

// type CurrentModal = ?ModalName
const { currentModal }  = this.props.modal

Returns the name of the current modal, null if none is currently displayed or if you're displaying several modals, the name of the one on top of the stack (currently shown to the user).

Note: The "name of the current modal" refers to the key you used for that specific modal in the modal configuration object you used in createModalStack.

openModal - Open a modal

Use this to open any modal you've setup.

closeModal - close a modal

This function closes a modal. Depending on your modal stack configuration it will either: close the first modal in your stack or clear the whole stack (see createModalStack).

closeModals - close every instance of a given modal

React Native Modalfy workflow allows you to use the same modal component several times. Moreover, it also let you call and display the same modal several times.

Let's say that for some reason you have to display several error modals to your user, if they perform the action needed to fix everything: you don't want them to have to close all the modals one by one. Similarly, it'd be nice if you didn't have to programmatically close all of them: that's what closeModals is here for.

You'll just have to pass as an argument the name of the modal you'd like to close and all its instances will be deleted.

closeAllModals - close all opened modals

This function will close every single opened modal, clearing the whole stack.

addListener - subscribe to updates to modal lifecycle

As the name suggest, addListener allows you to hook listeners to your current modal. For now, only onAnimate is supported as a valid EventName. If you need to perform any animation at the same time as your modal, this function is what you'll need.

If you're looking for a specific listener EventName that's not implemented yet: feel free to submit an issue on the repo

removeAllListeners - remove all modal listeners

As seen in the previous section, addListener returns an object with a remove() function in it. However if you have a lot of listeners subscribed to the same modal component, calling listenerA.remove(), listernerB.remove(), listernerC.remove() isn't ideal. That's what removeAllListeners is here for: just call it once and all the modal listeners will be removed.

params - current modal's params

This is the other way to access a modal's params. Here you'll directly have access to the data you passed.

getParams - get current modal's params with fallback

Note: getParams is really useful when you're not sure if your params content will be there for any reason (coming from an API, output of an async function, etc).

Last updated

Was this helpful?