I have eventData which I need to pass from page A to B. Here's my code:
function eCE(eventData) {
history.push({
pathname: '/ece',
state: eventData
})
})
This function gets triggered on button click, so it passed eventData through to the function. Now I need to send eventData through to the next page, in which I would get to by calling /ece. However, I am trying to grab the state on the other page like so:
const { state } = this.props.location
console.log(state)
it isn't working, and the error I get is:
TypeError: undefined has no properties
not to sure what to do here. appreciate the help !
In page 1:
import { useHistory } from "react-router-dom";
function page1(){
const history = useHistory();
const goToPage2 = () => {
history.push("/page2", { age: 18 });
}
return <button onClick={goToPage2}>Go To Page2</button>
}
In page 2:
import { useLocation } from "react-router-dom";
function page2(){
const location = useLocation();
return <div><p>{location.state.age}</p></div>
}