11import React , { Component , Fragment , useState , useEffect } from 'react'
22import { createPortal } from 'react-dom'
33import { Provider , useDispatch , useSelector } from 'react-redux'
4- import { Router , BrowserRouter , Switch , Route } from 'react-router-dom'
4+ import { Router , BrowserRouter , Switch , Route , useLocation } from 'react-router-dom'
55import { createBrowserHistory } from 'history'
66import { createStore , applyMiddleware } from 'redux'
77import thunk from 'redux-thunk'
@@ -73,6 +73,17 @@ const Categories = () => {
7373 return < div > Categories</ div >
7474}
7575
76+ const PageUsingLocationState = ( ) => {
77+ const [ title , setTitle ] = useState ( )
78+ const location = useLocation ( )
79+
80+ useEffect ( ( ) => {
81+ if ( location . state ) setTitle ( location . state . title )
82+ } , [ location . state ] )
83+
84+ return < div > { title } </ div >
85+ }
86+
7687export const history = createBrowserHistory ( )
7788
7889export const MyAppWithRouting = ( ) => {
@@ -86,6 +97,12 @@ export const MyAppWithRouting = () => {
8697 component = { Categories }
8798 exact = { true }
8899 />
100+ < Route
101+ component = { PageUsingLocationState }
102+ key = "page-using-location-state"
103+ path = "/page-using-location-state"
104+ exact = { true }
105+ />
89106 </ Switch >
90107 </ Router >
91108 )
0 commit comments