-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use withRouter in createConnectedLink - Switch order of arguments to isActive - Use custom pattern for injecting additional props from containers
- Loading branch information
Showing
11 changed files
with
102 additions
and
62 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
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,3 +1,3 @@ | ||
import createConnectedLink from './createConnectedLink'; | ||
|
||
export default createConnectedLink({}); | ||
export default createConnectedLink(); |
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
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,17 +1,8 @@ | ||
import { connect } from 'react-redux'; | ||
|
||
import BaseLink from './BaseLink'; | ||
import createWithRouter from './createWithRouter'; | ||
import defaultWithRouter from './withRouter'; | ||
|
||
export default function createConnectedLink({ | ||
getMatch = ({ match }) => match, | ||
}) { | ||
return connect( | ||
state => ({ match: getMatch(state) }), | ||
null, | ||
(stateProps, dispatchProps, ownProps) => ({ | ||
...ownProps, | ||
...stateProps, | ||
// We don't want dispatch here. | ||
}), | ||
)(BaseLink); | ||
export default function createConnectedLink(options) { | ||
const withRouter = options ? createWithRouter(options) : defaultWithRouter; | ||
return withRouter(BaseLink); | ||
} |
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
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,52 @@ | ||
import { connect } from 'react-redux'; | ||
|
||
import { routerShape } from './PropTypes'; | ||
|
||
const routerContextTypes = { | ||
router: routerShape.isRequired, | ||
}; | ||
|
||
export default function createWithRouter({ | ||
getMatch = ({ match }) => match, | ||
}) { | ||
const withMatch = connect( | ||
state => ({ match: getMatch(state) }), | ||
null, | ||
(stateProps, dispatchProps, ownProps) => ({ | ||
...ownProps, | ||
...stateProps, | ||
// We don't want dispatch here. | ||
}), | ||
); | ||
|
||
return function withRouter(Component) { | ||
const ConnectedComponent = withMatch(Component); | ||
|
||
// Yes, this is pretty gross. It's the simplest way to inject router as | ||
// a prop without adding yet another wrapper component, though. | ||
|
||
ConnectedComponent.contextTypes = { | ||
...ConnectedComponent.contextTypes, | ||
...routerContextTypes, | ||
}; | ||
|
||
// Overwriting the method instead of extending the class is used to avoid | ||
// issues with compatibility on IE <= 10. | ||
const baseAddExtraProps = ConnectedComponent.prototype.addExtraProps; | ||
|
||
function addExtraProps(props) { | ||
return { | ||
...baseAddExtraProps.call(this, props), | ||
|
||
// It's safe to read from the context because the router context | ||
// methods should never change. With the default implementation, they | ||
// in fact can't change. | ||
router: this.context.router, | ||
}; | ||
} | ||
|
||
ConnectedComponent.prototype.addExtraProps = addExtraProps; | ||
|
||
return ConnectedComponent; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import createWithRouter from './createWithRouter'; | ||
|
||
export default createWithRouter({}); |