@@ -6,6 +6,61 @@ import {
66 Link
77} from 'react-router-dom'
88
9+ // This example shows how to render two different screens
10+ // (or the same screen in a different context) at the same url,
11+ // depending on you got there.
12+ //
13+ // Click the colors and see them full screen, then "visit the
14+ // gallery" and click on the colors. Note the URL and the component
15+ // are the same as before but now we see them inside a modal
16+ // on top of the old screen.
17+
18+ class ModalSwitch extends React . Component {
19+
20+ // We can pass a location to <Switch/> that will tell it to
21+ // ignore the router's current location and use the location
22+ // prop instead.
23+ //
24+ // We can also use "location state" to tell the app the user
25+ // wants to go to `/images/2` in a modal, rather than as the
26+ // main page, keeping the gallery visible behind it.
27+ //
28+ // Normally, `/images/2` wouldn't match the gallery at `/`.
29+ // So, to get both screens to render, we can save the old
30+ // location and pass it to Switch, so it will think the location
31+ // is still `/` even though its `/images/2`.
32+ previousLocation = this . props . location
33+
34+ componentWillUpdate ( nextProps ) {
35+ const { location } = this . props
36+ // set previousLocation if props.location is not modal
37+ if (
38+ nextProps . history . action !== 'POP' &&
39+ ( ! location . state || ! location . state . modal )
40+ ) {
41+ this . previousLocation = this . props . location
42+ }
43+ }
44+
45+ render ( ) {
46+ const { location } = this . props
47+ const isModal = ! ! (
48+ location . state &&
49+ location . state . modal &&
50+ this . previousLocation !== location // not initial render
51+ )
52+ return (
53+ < div >
54+ < Switch location = { isModal ? this . previousLocation : location } >
55+ < Route exact path = '/' component = { Home } />
56+ < Route path = '/gallery' component = { Gallery } />
57+ < Route path = '/img/:id' component = { ImageView } />
58+ </ Switch >
59+ { isModal ? < Route path = '/img/:id' component = { Modal } /> : null }
60+ </ div >
61+ )
62+ }
63+ }
964
1065const IMAGES = [
1166 { id : 0 , title : 'Dark Orchid' , color : 'DarkOrchid' } ,
@@ -16,10 +71,18 @@ const IMAGES = [
1671]
1772
1873const Thumbnail = ( { color } ) =>
19- < div style = { { width : 50 , height : 50 , background : color } } > </ div >
74+ < div style = { {
75+ width : 50 ,
76+ height : 50 ,
77+ background : color
78+ } } />
2079
2180const Image = ( { color } ) =>
22- < div style = { { width : '100%' , height : 400 , background : color } } > </ div >
81+ < div style = { {
82+ width : '100%' ,
83+ height : 400 ,
84+ background : color
85+ } } > </ div >
2386
2487const Home = ( ) => (
2588 < div >
@@ -34,17 +97,19 @@ const Home = () => (
3497
3598const Gallery = ( ) => (
3699 < div >
37- {
38- IMAGES . map ( i => (
39- < Link
40- key = { i . id }
41- to = { { pathname : `/img/${ i . id } ` , state : { modal : true } } }
42- >
43- < Thumbnail color = { i . color } />
44- < p > { i . title } </ p >
45- </ Link >
46- ) )
47- }
100+ { IMAGES . map ( i => (
101+ < Link
102+ key = { i . id }
103+ to = { {
104+ pathname : `/img/${ i . id } ` ,
105+ // this is the trick!
106+ state : { modal : true }
107+ } }
108+ >
109+ < Thumbnail color = { i . color } />
110+ < p > { i . title } </ p >
111+ </ Link >
112+ ) ) }
48113 </ div >
49114)
50115
@@ -102,41 +167,6 @@ const Modal = ({ match, history }) => {
102167 )
103168}
104169
105- class ModalSwitch extends React . Component {
106-
107- componentWillMount ( ) {
108- // set the initial previousLocation value on mount
109- this . previousLocation = this . props . location
110- }
111-
112- componentWillUpdate ( nextProps ) {
113- const { location } = this . props
114- // set previousLocation if props.location is not modal
115- if ( nextProps . history . action !== 'POP' && ( ! location . state || ! location . state . modal ) ) {
116- this . previousLocation = this . props . location
117- }
118- }
119-
120- render ( ) {
121- const { location } = this . props
122- const isModal = ! ! (
123- location . state &&
124- location . state . modal &&
125- this . previousLocation !== location
126- )
127- return (
128- < div >
129- < Switch location = { isModal ? this . previousLocation : location } >
130- < Route exact path = '/' component = { Home } />
131- < Route path = '/gallery' component = { Gallery } />
132- < Route path = '/img/:id' component = { ImageView } />
133- </ Switch >
134- { isModal ? < Route path = '/img/:id' component = { Modal } /> : null }
135- </ div >
136- )
137- }
138- }
139-
140170const ModalGallery = ( ) => (
141171 < Router >
142172 < Route component = { ModalSwitch } />
0 commit comments