Skip to content

Commit d2ff84a

Browse files
authored
Add an example on how to use the onEnter callback
This change adds an example on how to use the onEnter callback to redirect a user to another page.
1 parent 7e643dc commit d2ff84a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/API.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,28 @@ Called when a route is about to be entered. It provides the next router state an
345345

346346
If `callback` is listed as a 3rd argument, this hook will run asynchronously, and the transition will block until `callback` is called.
347347

348+
###### `callback` signature
349+
`cb(err)`
350+
351+
```js
352+
const userIsInAnOrganisation = (nextState, replace, callback) => {
353+
fetch(...)
354+
.then(response = response.json())
355+
.then(userOrganisations => {
356+
if (userOrganisations.length === 0) {
357+
replace({
358+
pathname: '/users/' + nextState.params.userId + '/organisations/new',
359+
state: { nextPathname: nextState.location.pathname }
360+
})
361+
362+
callback(null);
363+
}
364+
})
365+
}
366+
367+
<Route path="/users/:userId/organisations" onEnter={userIsInAnOrganisation} />
368+
```
369+
348370
##### `onChange(prevState, nextState, replace, callback?)`
349371
Called on routes when the location changes, but the route itself neither enters or leaves. For example, this will be called when a route's children change, or when the location query changes. It provides the previous router state, the next router state, and a function to redirect to another path. `this` will be the route instance that triggered the hook.
350372

0 commit comments

Comments
 (0)