2016-05-03 54 views
1

我必須使用相同的控制器幾條路線:陣營:調度不開火路由變化

<Route component={Search} path='/accommodation(/:state)(/:region)(/:area)' /> 

,在道路改變我調用API函數從部件中:

componentWillReceiveProps = (nextProps) => { 
    if (this.props.params != nextProps.params) { 
     loadSearch(nextProps.params); 
    } 
} 

這是一個動作如下:

export function loadSearch (params) { 
    return (dispatch) => { 
     return dispatch(
      loadDestination(params) 
     ).then(() => { 
      return dispatch(
       loadProperties(params) 
      ); 
     }); 
    }; 
} 

它加載:

export const DESTINATION_REQUEST = 'DESTINATION_REQUEST'; 
export const DESTINATION_SUCCESS = 'DESTINATION_SUCCESS'; 
export const DESTINATION_FAILURE = 'DESTINATION_FAILURE'; 

export function loadDestination (params) { 
    const state = params.state ? `/${params.state}` : ''; 
    const region = params.region ? `/${params.region}` : ''; 
    const area = params.area ? `/${params.area}` : ''; 

    return (dispatch) => { 
     return api('location', {url: `/accommodation${state}${region}${area}`}).then((response) => { 
      const destination = formatDestinationData(response); 

      dispatch({ 
       type: DESTINATION_SUCCESS, 
       destination 
      }); 
     }); 
    }; 
} 

export const PROPERTIES_REQUEST = 'PROPERTIES_REQUEST'; 
export const PROPERTIES_SUCCESS = 'PROPERTIES_SUCCESS'; 
export const PROPERTIES_FAILURE = 'PROPERTIES_FAILURE'; 

export function loadProperties (params, query, rows = 24) { 
    return (dispatch, getState) => { 

     const locationId = getState().destination.id || 99996; 

     return api('search', {locationId, rows}).then((response) => { 
      const properties = response.results.map(formatPropertiesData); 

      dispatch({ 
       type: PROPERTIES_SUCCESS, 
       properties 
      }); 
     }); 
    }; 
} 

在初始頁面加載這個工作,並從api返回數據並呈現內容。然而,在改變路線時,loadSearch函數被觸發,但dispatch(它返回實際數據)不會。

+0

請寄出'loadDestination'和'loadProperties'的代碼。 –

+0

@bhargavponnapalli - 增加了這些功能。通過在'console.log'中添加任意一個,我可以看到最初的頁面加載,但不會在路由更改時再次觸發 – Paul

回答

2

請將您的代碼改爲此。你錯過了dispatch

假設:您正在使用redux-thunk,並且組件可以通過道具(已連接)進行分派。既然你提到你正在頁面加載調度,我認爲是這樣。

componentWillReceiveProps = (nextProps) => { 
    const {dispatch} = this.props; 
    if (this.props.params != nextProps.params) { 
     nextProps.dispatch(loadSearch(nextProps.params)); 
    } 
}