反应本机REDX传奇如何向用户显示错误
问题描述:
我是新的反应原生和反应传奇。我有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())
}
}
不知道我怎么能传递错误回到容器页面显示错误。任何帮助将不胜感激。在此先感谢
答
当你调用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
访问错误在你的UI有什么地方可以阅读如何设置maStateToProps。抱歉,仍然试图学习所有这些东西。谢谢 – dogwasstar
@ user767018 https://egghead.io/courses/getting-started-with-redux丹·阿布拉莫夫是Redux的发明者。 'mapStateToProps'在'connect()'课程中,但我建议从头开始。 – FuzzyTree