I want to pass some value from one component to another once the user clicks a button.
First Component (Survey
) with a value survey
--> User clicks a button --> Second component (Details
) renders the survey
To achieve so I am using Redirect
from the react-router-dom
.
function Survey({survey}) {
....
// There is a button that triggers the below if clause. The redirect works but it seems the state is not sent.
if (surveyPage === true) {
return <Redirect to={{
pathname: '/detail',
state: { location: survey }
}}
}
return("something")
}
Second component
function Details(){
console.log( this.props.location.state.location) // Cannot read property 'props' of undefined
return "test"
}
Cannot read property 'props' of undefined
I tried also:
function Details({survey){
console.log(survey) // Undefined
return "test"
}
and
function Details({ location: survey }){
console.log(survey) // Undefined
return "test"
}
I am really not understanding what I am doing wrong. I am following the documentations: https://reacttraining.com/react-router/web/guides/primary-components