# Listeners plugin

{% hint style="info" %}
`router.subscribe` is now available and as a result listeners plugin is no longer needed by `react-router5`. This will be deprecated in a near future.
{% endhint %}

## Usage

```javascript
import listenersPlugin from 'router5-plugin-listeners'

const router = createRouter()

router.usePlugin(listenersPlugin())
```

## Types of listeners

Listeners are called with `toState` and `fromState` arguments.

### Listen to a node change

`addNodeListener(name, fn)` will register a listener which will be invoked when the specified route node is the **transition node** of a route change, i.e. the intersection between deactivated and activated segments.

## Listen to any route change

Listeners registered with `addListener(fn)` will be triggered on any route change, including route reloads (*toState* will be equal to *fromState*). You can remove a previously added listener by using `removeListener(fn)`.

## Listen to a specific route

`addRouteListener(name, fn)` will register a listener which will be triggered when the router is navigating to the supplied route name.


---

# Agent Instructions: 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:

```
GET https://router5.js.org/advanced/listeners-plugin.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
