2017-08-19 43 views
0

我一直試圖從外匯交易API中提取數據,但我有點難以理解爲什麼更改沒有通過mapStateToProps反映出來。Redux Store已更新,但未反映在mapStateToProps

實質上,我試圖通過動作創建者調用api。這是動作創建者。

export const fetchTimeData = (currency, date) => { 
    return async (dispatch) => { 
    const res = await axios.get(`http://api.fixer.io/${date}?base=${currency}`); 

    const temp = res.data.rates; 
    var arr = Object.keys(temp).map(function (key) { 
     return (
      temp[key] 
    ); 
    }); 

    var arr2 = Object.keys(temp).map(function (key) { 
     return (
      key 
    ); 
    }); 

    var empty = []; 

    for(var i = 0; i < arr.length; i++){ 
     empty[i] = {title: arr2[i], value: arr[i]}; 
    } 

    _.remove(empty, {title: 'IDR'}); 
    _.remove(empty, {title: 'KRW'}); 
    _.remove(empty, {title: 'HUF'}); 

    empty.sort((a, b) => { 
     var titleA = a.title.toLowerCase() 
     var titleB = b.title.toLowerCase() 
     if (titleA < titleB) //sort string ascending 
      return -1 
     if (titleA > titleB) 
      return 1 
     return 0 //default return value (no sorting) 
    }) 

    dispatch({type: FETCH_TIME_DATA, payload: empty}); 
    } 
}; 

此操作正從另一個文件中的calculateDays函數中調用。 enumerateDays函數返回所需日期和當前日期之間的日期數組。例如,[「2017-08-10」,「2017-08-11」,「2017-08-12」,「2017-08-13」,「2017-08-14」,「2017-08-15 「,」2017-08-16「,」2017-08-17「,」2017-08-18「,」2017-08-19「]

在calculateDays中,正在調用動作創建器,轉到是我使用的API的異步調用。

componentWillMount() { 
    this.calculateDays(); 
} 

calculateDays() { 
    var currentDate = moment(); 
    var hold = enumerateDays('2017-8-10', currentDate); 

    var days = []; 
    var firstDay = hold[0]; 
    var currencies; 

    days = hold.map((date) => { 

     var inBetween = calculateBetween(firstDay, date); 
     this.props.fetchTimeData(this.props.base, date); 

     console.log("this is tempData", this.props.saveTime) 

     return(
      { 
       currencies: 'test', 
       date: date, 
       days: inBetween 
      } 
     ) 
    }) 

} 

render(){ 

    const margins = { top: 50, right: 20, bottom: 100, left: 60 }; 
    const svgDimensions = { width: 1400, height: 800 }; 

    var data = [ 
     { 
      "age": 39, 
      "index": 0 
     }, 
     { 
      "age": 38, 
      "index": 1 
     }, 
     { 
      "age": 34, 
      "index": 2 
     }, 
     { 
      "age": 12, 
      "index": 3 
     } 
    ]; 

    //for color, pass the array of colors to the redux store then pop off from the beginning into chartSeries 

    var chartSeries = [ 
     { 
     field: 'age', 
     name: 'USD', 
     color: '#ff7f0e', 
     style: { 
      "stroke-width": 2, 
      "stroke-opacity": .2, 
      "fill-opacity": .2 
     } 
     } 
    ] 

    //iterate over a list of years and calculate days from using moment 
    //the data will have years, but the function down here will change it 
    //set the very first index date as the "from" date in moment.js 
    var x = function(d) { 
     return d.index; 
    } 

    return(
     <div> 
      <LineChart 
       margins= {margins} 
       width={svgDimensions.width} 
       height={svgDimensions.height} 
       data= {data} 
       chartSeries= {chartSeries} 
       x= {x} 
      /> 
      {console.log(this.props.saveTime)} 
     </div> 
    ); 
} 

function mapStateToProps(state) { 
    return { 
     saveTime: state.data.currencyTime 
    }; 
} 

export default connect(mapStateToProps, actions)(DateChart); 

最後,這是我的減速器。

const INITIAL_STATE = { 
    currency: fakeData, 
    currencyTime: [] 
} 

export default function(state = INITIAL_STATE, action) { 
    switch (action.type){ 
     case FETCH_DATA: 
      return {...state, currency: action.payload}; 
     case FETCH_TIME_DATA: 
      return {...state, currencyTime: action.payload}; 
     default: 
      return state; 
    } 
} 

我試着終極版記錄儀調試和我看到的動作被正確http://imgur.com/a/HGHbB解僱。但是,我仍然返回一個空數組。

在我的其他類似的行動創造者,我從api和mapeStateTo道具沒有問題的數據。

我覺得我缺少了解組件生命週期中redux存儲庫更新的關鍵部分,但我不確定究竟是什麼。回購是在這裏,如果你想仔細看看https://github.com/kudou-reira/forexD3

+2

我沒有看到你使用currencyTime和saveTime組件中的任何地方,那麼你怎麼知道它沒有反映出來?你是否在組件的某個地方執行console.log以查看它是否已更新? –

回答

0

你有沒有添加你reducer在你的combineReducer?

+0

組件的'mapSTateToProps'中的數據即將到來,但無法在渲染函數中看到對任何道具的引用 – Ankush

+0

您可以與您的代碼分享您在mapStateToProps函數中編寫的內容嗎?並檢查您是否已將mapStateToProps傳遞給連接,即導出默認連接(mapStateToProps,{})(ComponentName) –

+0

請自行查找https://github.com/kudou-reira/forexD3/blob/master/src/components/dateChart。 js PS這不是我的代碼 – Ankush

相關問題