# Preventing navigation

It is a common case to want to allow / prevent navigation away from a view or component: if a User is in the middle of completing a form and data has not been saved, you might want to warn them about data being lost or prevent them to leave the current view until data has been saved.

## Using lifecycle functions

Router5 supports `canActivate` and `canDeactivate` functions for route segments:

* `canActivate` functions are called on segments which will become active as a result of a route change
* `canDeactivate` functions are called on segments which will become inactive as a result of a route change

Both functions have the same signature than middleware functions. Their result can be synchronous (returning a boolean) or asynchronous (returning a promise or calling `done(err, result)`).

{% hint style="info" %}
if a canActivate or canDeactivate function doesn't return a boolean, a promise or doesn't call back, the transition will not proceed.
{% endhint %}

```javascript
const canActivate = (router) => (toState, fromState) => {
    return true;
}

router.canActivate('admin', canActivate);
```

`canActivate` functions are called from top to bottom on newly activated segments. `canDeactivate` methods are invoked from bottom to top.

### Using middleware functions

Middleware functions behave like `canActivate` and `canDeactivate`. Read more about [middleware](/advanced/middleware.md).


---

# 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/preventing-navigation.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.
