> For the complete documentation index, see [llms.txt](https://colorfy-software.gitbook.io/react-native-modalfy/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://colorfy-software.gitbook.io/react-native-modalfy/guides/triggering-a-callback.md).

# Triggering a callback

#### [> ModalProp API](/react-native-modalfy/api/types/modalprop.md)

You may often want to perform an action as soon as you open or close a modal. Up until now, you had to skillfully employ `setTimeout()` in order to trigger your function after the modal is done animating. With Modalfy v3, you no longer have to think about it thanks to the built-in support of callbacks.

## 1. When opening

The [**`openModal()`**](/react-native-modalfy/api/types/modalprop.md#openmodal) function now accepts a 3rd argument that designates your callback. Simply provide it and you should see it be invoked *after* the modal has been opened (this includes animation time as well).

{% tabs %}
{% tab title="TypeScript" %}

```typescript
openModal('WelcomeModal', undefined, () => {
  console.log(`✅ Opened WelcomeModal`)
})

openModal('ErrorModal', { titleColor: 'red' }, () => {
  closeModal('WelcomeModal')
})
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
As you can see, [**`openModal()`**](/react-native-modalfy/api/types/modalprop.md#openmodal)still expects the`modalName`and`params`as the first two arguments, with the latter being optional.
{% endhint %}

## 2. When closing

As you'd expect, all the closing methods have also received a new argument that behaves in the same capacity:

{% tabs %}
{% tab title="TypeScript" %}

```typescript
closeModal('ErrorModal', () => openModal('LoginModal'))

closeModals('ErrorModal', () => console.log('✅ Closed all ErrorModal'))

closeAllModals(() => console.log('✅ Closed all modals'))
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
You'll notice that [**`closeModal()`**](/react-native-modalfy/api/types/modalprop.md#closemodal) first argument is optional. That's you can either provide `undefined` (to close the latest opened modal) or specify a name to close another one.
{% endhint %}

## Things to remember

If you're using [**`animationIn`**](/react-native-modalfy/api/types/modaloptions.md#animationin)/[**`animationOut`**](/react-native-modalfy/api/types/modaloptions.md#animationout) to drive the modal animations yourself, remember that you have to call the new `callback` argument there in order for your callbacks to be called. You can refer to the dedicated section in the [**Upgrading from v2.x**](https://colorfy-software.gitbook.io/react-native-modalfy/guides/pages/-M85bBxrLPKNbcdxCZyz#modaloptions.animateout-new-mandatory-callback-argument) guide to learn more about it.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://colorfy-software.gitbook.io/react-native-modalfy/guides/triggering-a-callback.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
