2017-09-04 65 views
0
中提取變量

我有一個運行reactjs的多頁web應用程序。 我試圖定義一個定製的反應 - 麪包屑特定頁面 涉及提取值,並使用在麪包屑React-BreadCrumbs不會從方法

運行與下面的代碼,我可以看到控制檯拿起作業名 但如果我看着麪包屑,我被卡住了

home > templates > Missing name prop from Route 

我不太明白爲什麼變量沒有被路由器接收。如果我只是硬編碼,它會起作用。有什麼建議?

getTemplateJobName(templateId,dateChosen){ 
    doGetJobById({jobId: templateId,reconDate: dateChosen}).then(
    ({body: template})=>{ 
     let {jobName: jobName}=template; 
     console.log(jobName); 
     return jobName; 
    }); 
}, 

render(){ 
    return (
    <div> 
    <Router history={browserHistory}> 
     <Route name='home' path={'/'+contextName}> 
      <IndexRoute component={LandingPage}/> 
      <Route name='templates' path='templates'> 
       <IndexRoute component={JobPage}/> 
       <Route path=':reconDate&:templateId' component={JobDetailPage} staticName={true} getDisplayName={(params) => this.getTemplateJobName(params.templateId,params.reconDate)}/> 
      </Route> 
      <Route name='report' path='report' component={ReportPanel}/> 
     </Route> 
     <Route path='*' component={NotFoundPage}/> 
    </Router> 
+0

不必返回上getDisplayName道具的價值。嘗試'getDisplayName = {(params)=>(this.getTemplateJobName(params.templateId,params.reconDate))}' – bennygenel

+0

@bennygenel似乎沒有讓傷心的區別。仍然打印到控制檯,但沒有看到它在麪包屑(主頁>模板>從路線缺少名稱道具) – user3120554

+0

是否承諾功能不正確? – user3120554

回答

0

請嘗試此操作;

export default App extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     DisplayName: '' 
    }; 
    } 

    getTemplateJobName(templateId,dateChosen) { 
    doGetJobById({jobId: templateId,reconDate: dateChosen}).then(
     ({body: template})=>{ 
     let {jobName: jobName}=template; 
     console.log(jobName); 
     // return jobName; 
     this.setState({ DisplayName: jobName }); 
     } 
    ); 
    } 

    getDisplayName(params) { 
    this.getTemplateJobName(params.templateId,params.reconDate); 
    return this.state.DisplayName; 
    } 


    render() { 
    return 
    (<div> 
     <Router history={browserHistory}> 
      <Route name='home' path={'/' + contextName}> 
      <IndexRoute component={LandingPage}/> 
      <Route name='templates' path='templates'> 
      <IndexRoute component={JobPage}/> 
      <Route path=':reconDate&:templateId' component={JobDetailPage} staticName={true} getDisplayName={ this.getDisplayName.bind(this) }/> 
      </Route> 
      <Route name='report' path='report' component={ReportPanel}/> 
     </Route> 
     <Route path='*' component={NotFoundPage}/> 
     </Router> 
    </div>) 
    } 
}