0
我有這樣的情況,我的反應路由器:加載狀態反應
root.js
<Route path="job-opportunity" component={Opportunity} >
<Route path=":opportunitySlug" />
</Route>
當我打開domain.com/job-opportunity一切工作正常 但是,當我打兒童路線domain.com/job-opportunity/some-slug
它不加載我在兒童組件中使用的機會組件的一些構造函數狀態。
讓我告訴你我的機會組件:
class Opportunity extends React.Component{
constructor(props){
super(props);
this.loadOpportunities = this.loadOpportunities.bind(this);
this.state = {
'opportunities' : []
};
this.loadOpportunities();
}
loadOpportunities(){
$.get($('.api_router_load_opportunities').val(), function(data){
this.setState({'opportunities' : data});
}.bind(this));
}
render() {
return (
<div>
<h1>Layout!</h1>
<Content
opportunities={this.state.opportunities}
loggedUser={this.props.loggedUser}
opportunitySlug={this.props.params.opportunitySlug}
/>
</div>
)
}
}
function Content(props){
const opportunitySlug = props.opportunitySlug;
if(opportunitySlug){
return <SingleOpportunity
opportunities={props.opportunities}
loggedUser={props.loggedUser}
opportunitySlug={opportunitySlug}
/>;
}else{
return <ListOpportunity
opportunities={props.opportunities}
loggedUser={props.loggedUser}
/>;
}
}
出口默認機會;
domain.com/job-opportunity/some-slug
但當:
基本上當我打domain.com/job-opportunity然後在瀏覽器中的一切鏈接導航,即使在這條線路工作正常我直接打在瀏覽器domain.com/job-opportunity/some-slug
它brokes因爲我在SingleOpportunity this.props.opportunities渲染[this.props.opportunitySlug]。名稱