2017-10-17 74 views
0

我是新的反應原生和反應傳奇。我有api電話工作,並且據我所知,一切都按預期工作。但是,如何顯示我從服務器返回給用戶的錯誤。對不起,如果這是一個微不足道的問題。這裏是我到目前爲止的代碼反應本機REDX傳奇如何向用戶顯示錯誤

集裝箱/ GetUserInfo.js

moveToJoinScreen(){ 
    this.props.getStudentAvator(vouchercode, DeviceInfo.getUniqueID(), 'iOS'); 
} 

render() { 
    return (
     <View style={styles.container}> 
     <TouchableHighlight onPress={() => this.moveToJoinScreen()} underlayColor='#ff000000'> 
      <Image style={styles.imagestyle} style={{height:60, width:150}} source={Images.joinNow} /> 
     </TouchableHighlight> 
     <Text style={styles.welcome}> 
      {'\n'} 
     </Text> 
     <TouchableHighlight onPress={() => this.moveToRegisterScreen()} underlayColor='#ff000000'> 
      <Image style={styles.imagestyle} style={{height:80, width:150}} source={Images.registerMyId} /> 
     </TouchableHighlight> 
     </View> 
    ) 
    } 
} 

const mapDispatchToProps = dispatch => ({ 
    getStudentAvator:(emailaddress, deviceid, platform) => dispatch(StudentActions.SetupVoucherRequest(vouchercode, deviceid, platform)) 
}) 

export default connect(null, mapDispatchToProps)(LaunchScreen) 

這裏是saga.js

export function* SetupVoucherRequest(api, action) { 
    const { vouchercode, deviceid, platform } = action 
    if(vouchercode.length >11 || vouchercode.length < 10) 
    { 
     yield put(StudentCardActions.SetupVoucherFailuer()) 
     return 
    } 
    // make the call to the api 
    const response = yield call(api.SetupVoucher, vouchercode, deviceid, platform) 

    if (response.ok) { 
     const firstUser = 'fahad' 
     const avatar = 'Avatar_Fahad' 
     // do data conversion here if needed 
     yield put(StudentCardActions.SetupVoucherSuccess(avatar)) 
    } else { 
     yield put(StudentCardActions.SetupVoucherFailuer()) 
    } 
} 

不知道我怎麼能傳遞錯誤回到容器頁面顯示錯誤。任何幫助將不勝感激。在此先感謝

回答

0

當你調用put(StudentCardActions.SetupVoucherFailuer())你應該用錯誤更新您的狀態,然後通過mapStateToProps

const mapStateToProps = state => ({ 
    error: state.myStoreName.error, 
    // depending on how your store is setup you might 
    // have to use state.get('myStoreName').error 
}) 

export default connect(mapStateToProps, mapDispatchToProps)(LaunchScreen) 

傳遞錯誤的UI現在,您可以通過this.props.error

+0

訪問錯誤在你的UI有什麼地方可以閱讀如何設置maStateToProps。抱歉,仍然試圖學習所有這些東西。謝謝 – dogwasstar

+0

@ user767018 https://egghead.io/courses/getting-started-with-redux丹·阿布拉莫夫是Redux的發明者。 'mapStateToProps'在'connect()'課程中,但我建議從頭開始。 – FuzzyTree