-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Add context.route.location #4589
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
Changes from all commits
0d704cd
c921cb0
adf11fd
9ace1c5
b2d946c
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 |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import React from 'react' | ||
import { | ||
BrowserRouter as Router, | ||
Switch, | ||
Route, | ||
Link | ||
} from 'react-router-dom' | ||
|
||
|
||
const IMAGES = [ | ||
{ id: 0, title: 'Dark Orchid', color: 'DarkOrchid' }, | ||
{ id: 1, title: 'Lime Green', color: 'LimeGreen' }, | ||
{ id: 2, title: 'Gold', color: 'Gold' }, | ||
{ id: 3, title: 'Midnight Blue', color: 'MidnightBlue' }, | ||
{ id: 4, title: 'Dark Slate Gray', color: 'DarkSlateGray' }, | ||
{ id: 5, title: 'Tomato', color: 'Tomato' }, | ||
{ id: 6, title: 'Seven Ate Nine', color: '#789' }, | ||
{ id: 7, title: 'Olive Drab', color: 'OliveDrab' }, | ||
{ id: 8, title: 'Crimson', color: 'Crimson' }, | ||
{ id: 9, title: 'Sea Green', color: 'SeaGreen' } | ||
] | ||
|
||
const Thumbnail = ({ color }) => | ||
<div style={{ width: 50, height: 50, background: color }}></div> | ||
|
||
const Image = ({ color }) => | ||
<div style={{ width: '100%', height: 400, background: color }}></div> | ||
|
||
const Home = () => ( | ||
<div> | ||
<Link to='/gallery'>Visit the Gallery</Link> | ||
<h2>Featured Images</h2> | ||
<ul> | ||
<li><Link to='/img/5'>Tomato</Link></li> | ||
<li><Link to='/img/9'>Sea Green</Link></li> | ||
</ul> | ||
</div> | ||
) | ||
|
||
const Gallery = () => ( | ||
<div> | ||
{ | ||
IMAGES.map(i => ( | ||
<Link | ||
key={i.id} | ||
to={{ pathname: `/img/${i.id}`, state: { modal: true} }} | ||
> | ||
<Thumbnail color={i.color} /> | ||
<p>{i.title}</p> | ||
</Link> | ||
)) | ||
} | ||
</div> | ||
) | ||
|
||
const ImageView = ({ match }) => { | ||
const image = IMAGES[parseInt(match.params.id, 10)] | ||
if (!image) { | ||
return <div>Image not found</div> | ||
} | ||
|
||
return ( | ||
<div> | ||
<h1>{image.title}</h1> | ||
<Image color={image.color} /> | ||
</div> | ||
) | ||
} | ||
|
||
const Modal = ({ match, history }) => { | ||
const image = IMAGES[parseInt(match.params.id, 10)] | ||
if (!image) { | ||
return null | ||
} | ||
const back = (e) => { | ||
e.stopPropagation() | ||
history.goBack() | ||
} | ||
return ( | ||
<div | ||
onClick={back} | ||
style={{ | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
bottom: 0, | ||
right: 0, | ||
background: 'rgba(0, 0, 0, 0.15)' | ||
}} | ||
> | ||
<div className='modal' style={{ | ||
position: 'absolute', | ||
background: '#fff', | ||
top: 25, | ||
left: '10%', | ||
right: '10%', | ||
padding: 15, | ||
border: '2px solid #444' | ||
}}> | ||
<h1>{image.title}</h1> | ||
<Image color={image.color} /> | ||
<button type='button' onClick={back}> | ||
Close | ||
</button> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
class ModalSwitch extends React.Component { | ||
|
||
componentWillMount() { | ||
// set the initial previousLocation value on mount | ||
this.previousLocation = this.props.location | ||
} | ||
|
||
componentWillUpdate(nextProps) { | ||
const { location } = this.props | ||
// set previousLocation if props.location is not modal | ||
if (nextProps.history.action !== 'POP' && (!location.state || !location.state.modal)) { | ||
this.previousLocation = this.props.location | ||
} | ||
} | ||
|
||
render() { | ||
const { location } = this.props | ||
const isModal = !!( | ||
location.state && | ||
location.state.modal && | ||
this.previousLocation !== location | ||
) | ||
return ( | ||
<div> | ||
<Switch location={isModal ? this.previousLocation : location}> | ||
<Route exact path='/' component={Home}/> | ||
<Route path='/gallery' component={Gallery}/> | ||
<Route path='/img/:id' component={ImageView}/> | ||
</Switch> | ||
{ isModal ? <Route path='/img/:id' component={Modal} /> : null} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const ModalGallery = () => ( | ||
<Router> | ||
<Route component={ModalSwitch} /> | ||
</Router> | ||
) | ||
|
||
export default ModalGallery |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import matchPath from './matchPath' | |
*/ | ||
class Route extends React.Component { | ||
static contextTypes = { | ||
history: PropTypes.object.isRequired | ||
history: PropTypes.object.isRequired, | ||
route: PropTypes.object.isRequired | ||
} | ||
|
||
static propTypes = { | ||
|
@@ -31,26 +32,26 @@ class Route extends React.Component { | |
getChildContext() { | ||
return { | ||
route: { | ||
location: this.props.location || this.context.history.location, | ||
location: this.props.location || this.context.route.location, | ||
match: this.state.match | ||
} | ||
} | ||
} | ||
|
||
state = { | ||
match: this.computeMatch(this.props) | ||
match: this.computeMatch(this.props, this.context) | ||
} | ||
|
||
computeMatch({ computedMatch, location, path, strict, exact }) { | ||
computeMatch({ computedMatch, location, path, strict, exact }, { route }) { | ||
if (computedMatch) | ||
return computedMatch // <Switch> already computed the match for us | ||
|
||
const pathname = (location || this.context.history.location).pathname | ||
const pathname = (location || route.location).pathname | ||
|
||
return matchPath(pathname, { path, strict, exact }) | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
componentWillReceiveProps(nextProps, nextContext) { | ||
warning( | ||
!(nextProps.location && !this.props.location), | ||
'<Route> elements should not change from uncontrolled to controlled (or vice versa). You initially used no "location" prop and then provided one on a subsequent render.' | ||
|
@@ -62,15 +63,15 @@ class Route extends React.Component { | |
) | ||
|
||
this.setState({ | ||
match: this.computeMatch(nextProps) | ||
match: this.computeMatch(nextProps, nextContext) | ||
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. Need to use the next context object here. Otherwise when there is no |
||
}) | ||
} | ||
|
||
render() { | ||
const { match } = this.state | ||
const { children, component, render } = this.props | ||
const { history } = this.context | ||
const { location } = history | ||
const { history, route } = this.context | ||
const location = this.props.location || route.location | ||
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. Just making sure that the component rendered by the route receives the correct location. |
||
const props = { match, location, history } | ||
|
||
return ( | ||
|
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 had to fix a few things in the examples to get them working with the new
{ match, location, history }
props.