-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
remove subscriptions #4598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove subscriptions #4598
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,17 @@ import React, { PropTypes } from 'react' | |
import warning from 'warning' | ||
import matchPath from './matchPath' | ||
|
||
const computeMatch = (router, { location, computedMatch, path, exact, strict }) => | ||
computedMatch || matchPath((location || router.location).pathname, { path, exact, strict }) | ||
|
||
/** | ||
* The public API for matching a single path and rendering. | ||
*/ | ||
class Route extends React.Component { | ||
static contextTypes = { | ||
router: PropTypes.shape({ | ||
listen: PropTypes.func.isRequired | ||
}).isRequired | ||
router: PropTypes.object.isRequired | ||
} | ||
|
||
static childContextTypes = { | ||
router: PropTypes.object.isRequired | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like keeping |
||
} | ||
|
||
static propTypes = { | ||
|
@@ -29,37 +29,22 @@ class Route extends React.Component { | |
location: PropTypes.object | ||
} | ||
|
||
static childContextTypes = { | ||
router: PropTypes.object.isRequired | ||
state = { | ||
match: this.computeMatch(this.props) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep |
||
} | ||
|
||
getChildContext() { | ||
return { | ||
router: this.router | ||
router: { | ||
...this.context.router, | ||
match: this.state.match | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ added |
||
} | ||
} | ||
|
||
componentWillMount() { | ||
const parentRouter = this.context.router | ||
|
||
this.router = { | ||
...parentRouter, | ||
match: computeMatch(parentRouter, this.props) | ||
} | ||
|
||
// Start listening here so we can <Redirect> on the initial render. | ||
this.unlisten = parentRouter.listen(() => { | ||
Object.assign(this.router, parentRouter, { | ||
match: computeMatch(parentRouter, this.props) | ||
}) | ||
|
||
this.forceUpdate() | ||
}) | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're going to remove |
||
Object.assign(this.router, { | ||
match: computeMatch(this.router, nextProps) | ||
this.setState({ | ||
match: this.computeMatch(nextProps) | ||
}) | ||
|
||
warning( | ||
|
@@ -72,19 +57,23 @@ class Route extends React.Component { | |
) | ||
} | ||
|
||
componentWillUnmount() { | ||
this.unlisten() | ||
computeMatch(props) { | ||
const { location, computedMatch, path, exact, strict } = props | ||
const { router } = this.context | ||
const pathname = (location || router.location).pathname | ||
return computedMatch || matchPath(pathname, { path, exact, strict }) | ||
} | ||
|
||
render() { | ||
const { children, component, render } = this.props | ||
const props = { ...this.router } | ||
const { match } = this.state | ||
const props = { ...this.context.router, match } | ||
|
||
return ( | ||
component ? ( // component prop gets first priority, only called if there's a match | ||
props.match ? React.createElement(component, props) : null | ||
match ? React.createElement(component, props) : null | ||
) : render ? ( // render prop is next, only called if there's a match | ||
props.match ? render(props) : null | ||
match ? render(props) : null | ||
) : children ? ( // children come last, always called | ||
typeof children === 'function' ? ( | ||
children(props) | ||
|
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved
computeMatch
cause it felt cleaner as an instance method, only had to checkcontext.router.location
v.props.location
in one spot. Also, it's ourdoImperativeStuff
method of this component. We do it initially, and then when we get new props.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually like the method up here, and I like that it's just 1 line up here instead of 4 down below. No need to create the function for every instance. Once will do :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JavaScript prototypes use only one function per instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I was having to do the same logic to figure out the arguments to pass to computeMatch, so complexity was just moving up and repeated, now its only in one place.