Passing props through react router not available in componentDidMount?

Passing props through react router not available in componentDidMount?



I've passed props via the react router like this to the mentioned component:


<BrowserRouter>
<Switch>
<Route path="/product-page" exact render=(props) => ( <ShopPages ...props response=this.state.response/>)/>
</Switch>
</BrowserRouter>



I would like to access this props in componentDidMount() like something like this:


componentDidMount()


componentDidMount()
console.log(this.props.response)



The prop this.props.response is available in the component I can console.log it in the render(). I'm not sure why in the above scenario the console shows the array empty. I've tried to see if the data is available then show the data as so console.log( 'show', this.props.response && this.props.response) or by adding async as so:


this.props.response


console.log( 'show', this.props.response && this.props.response)


async


async componentDidMount()
const data = await this.props.response



But this does nothing too. Any help?





remove () of around your <shopPage ... > component that you rendering it , see if it changes ????
– a_m_dev
Aug 26 at 20:07


()


<shopPage ... >




1 Answer
1



I'm just going to assume that this.state.response is not a Promise, but is actually the data since you said it was an empty array, therefore async/await will do nothing.


this.state.response



ComponentDidMount() is only fired once when the component mounts and is NOT the place to perform actions based on prop updates that can happen after mount. So I would suggest passing the promise, and if that is not an option do something like


componentDidUpdate(prevProps, prevState)
if (!prevProps.response.length && this.props.response.length)
//Your code







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)