This repository has been archived by the owner on May 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split Link into LinkBase component and withRoute higher-order c…
…omponent
- Loading branch information
Showing
3 changed files
with
60 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import Link from './Link'; | ||
import BaseLink from './BaseLink'; | ||
import routeNode from './routeNode'; | ||
import RouterProvider from './RouterProvider'; | ||
import withRoute from './withRoute'; | ||
|
||
export default { | ||
Link, | ||
const Link = withRoute(BaseLink); | ||
|
||
export { | ||
BaseLink, | ||
routeNode, | ||
RouterProvider | ||
RouterProvider, | ||
withRoute, | ||
Link | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Component, createElement } from 'react'; | ||
import * as invariant from 'invariant'; | ||
|
||
function withRoute(BaseComponent) { | ||
class ComponentWithRoute extends Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
this.router = context.router; | ||
this.state = { | ||
previousRoute: null, | ||
route: this.router.getState() | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
invariant( | ||
this.router.registeredPlugins.LISTENERS, | ||
'[react-router5] missing plugin router5-listeners.' | ||
); | ||
|
||
this.listener = (toState, fromState) => this.setState({ previousRoute: fromState, route: toState }); | ||
this.router.addListener(this.nodeListener); | ||
} | ||
|
||
componentWillUnmout() { | ||
this.router.removeListener(this.listener); | ||
} | ||
|
||
render() { | ||
invariant( | ||
!props.router && !props.route && !props.previousRoute, | ||
'[react-router5] prop names `router`, `route` and `previousRoute` are reserved.' | ||
); | ||
|
||
return createElement(BaseComponent, { ...props, ...this.state, router: this.router }); | ||
} | ||
} | ||
|
||
return ComponentWithRoute; | ||
} | ||
|
||
export default withRoute; |