2016-06-09 39 views
0

我有一箇中間件設置檢查靜態需求,基本上是在頁面呈現前準備異步數據,我在嘗試傳遞我的api調用函數時遇到問題getWaitingListPosition令牌多數民衆贊成在裏面像這樣的路線:無法獲取組件類中的當前路由

export default class WaitingListPage extends React.Component { 
    static need = [ 
    getWaitingListPosition(this.props.routeParams.token) 
    ] 

    ... 
} 

我得到一個錯誤說cannot read props of undefined

這是使用反應路由器V2.4.1

網址,這個組件是從訪問:https://mywebsite.com/w/:token

回答

1

靜態方法無權訪問實例(this)屬性。你應該讓它支持routeParams,然後調用這個函數。

static need = (routeParams) => { 
    // make sure to return a promise 
    return getWaitingListPosition(routeParams.token) 
} 

更新

這將需要渲染......這樣的過程中被調用。

// assuming a promise, call the static method on the routed component 
RoutedComponent.need(routeParams) 
.then((data) => { 
    render(<RoutedComponent data={data}/>) 
} 
+0

我相信它改變這種做法不執行''getWaitingListPosition功能,另外'[]'應該換成'{}' – Ilja

+0

好一點,是我不好。你是否想要做一個onEnter類型的事件? –

+0

從某種意義上說,我試圖在組件渲染之前觸發這個函數,它從API獲取東西並繼續渲染 – Ilja

相關問題