Skip to content

Commit 5538899

Browse files
authored
Merge pull request #3660 from thaiphan/patch-1
Add an example on how to use the onEnter callback
2 parents 7e643dc + 2a82ef1 commit 5538899

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 userIsInATeam = (nextState, replace, callback) => {
353+
fetch(...)
354+
.then(response = response.json())
355+
.then(userTeams => {
356+
if (userTeams.length === 0) {
357+
replace(`/users/${nextState.params.userId}/teams/new`)
358+
}
359+
callback();
360+
})
361+
.catch(error => {
362+
// do some error handling here
363+
callback(error);
364+
})
365+
}
366+
367+
<Route path="/users/:userId/teams" onEnter={userIsInATeam} />
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)