reactjs - Not getting route params after navigating via react-router-4 -
after navigating via this.props.history.push
don't route params
in component
.
i have single component
getting rendered on both routes.
so going 1 route resulting component update
, not getting route params
in it.
if refresh page route params
this.props.history.push(`/pois/select/${lastaction.payload.poiid}`)
my routing component.
class routes extends react.component { render() { return <div> <switch > <route exact path="/pois/" component={ pois } mode='view' /> <route exact path="/pois/select/:poiid" component={ pois } mode='select' /> <route exact path="/pois/create" component={ pois } mode='create'/> <route exact path="/pois/update/:poiid" component={ pois } mode='update'/> </switch> </div>; }
use combination of componentwillreceiveprops
, componentdidmount
lifecycle method.
when first time component renders api call inside componentdidmount
method, , fetch data, this:
componentdidmount(){ // ajax call fetch data setstate store in state variable }
next time when thing changes in props, componentwillreceiveprops
method called, api call in that, this:
componentwillreceiveprops(nextprops){ console.log(nextprops.params, this.props.params); // nextprops.params print new params values // this.props.params print old params values // ajax call fetch data setstate store in state variable }
Comments
Post a Comment