こちらの質問で、Reduxを通してエラーモーダルを表示させるための質問をしたのですが、やはり上手くいかなかったので、再度質問させていただきます。
エラー
onError is not found.
Reducerの中にonError()を書いていないので当たり前ではあるのですが、Reducerの中にAlert文を書くのは不整合であるように感じます。
Reducerで起こしたonError()
アクションをどのようにすれば、View上のonError()を起こすのに繋がるのかが知りたいところです。
ソースコード
actions
JavaScript
1・・・ 2export const userCreate = ({ user }) => { 3 return (dispatch) => { 4 firebase.database().ref(`/users/`) 5 .set({ 6 user 7 }) 8 .then(() => { 9 dispatch({ type: 'user_save_success' }); 10 }); 11 }) 12 .catch(err => { 13 dispatch({ type: 'USER_SAVE_FAIL' }); 14 }); 15 } 16} 17・・・
reducer
JavaScript
1export default (state = initialState, action) => { 2 switch(action.type) { 3 case 'USER_UPDATE': 4 return { ...state, [action.payload.prop]: action.payload.value }; 5 case 'USER_SAVE_SUCCESS': 6 return { initialState, successModal: true }; 7 case 'USER_SAVE_FAIL': 8 return { ...state, failedModal: true }; 9 default: 10 return state; 11 } 12};
View(UserCreate.js)
JavaScript
1class CreditCard extends React.Component { 2 state = { 3 showModal: false, 4 error: null 5 }; 6 7 static navigationOptions = ({ navigation }) => { 8 const { params = {} } = navigation.state; 9 return { 10 headerTitle: 'ユーザーの新規作成', 11 headerLeft: ( 12 <TouchableOpacity 13 onPress={()=> navigation.goBack()} 14 style={styles.headerIcon} 15 > 16 <Icon name='chevron-left' size={28} style={{color: '#fff'}}/> 17 </TouchableOpacity> 18 ), 19 headerRight: ( 20 <TouchableOpacity 21 onPress={() => params.handleSave()} 22 > 23 <Text style={styles.headerBtn}>{I18n.t('save')}</Text> 24 </TouchableOpacity> 25 ), 26 headerStyle: { 27 backgroundColor: Theme.PRIMARY, 28 }, 29 headerTitleStyle: { 30 color: '#fff', 31 fontWeight: 'bold' 32 } 33 }; 34 }; 35 36 onButtonPress() { 37 const { firstName, lastName } = this.props; 38 39 this.props.userCreate({ firstName, lastName }); 40 } 41 42 componentDidMount() { 43 this.props.navigation.setParams({ handleSave: this.onButtonPress.bind(this) }); 44 } 45 46 render() { 47 if(this.props.error === true) { 48 Alert.alert( 49 'エラーが発生しました', 50 [ 51 {text: 'キャンセル', onPress: () => console.log('Cancel Pressed'), style: 'cancel'}, 52 {text: 'はい', onPress: () => this.onConfirmReport()}, 53 ], 54 { cancelable: false } 55 ) 56 } 57 58 return ( 59 <View> 60 <TextInput 61 value={this.props.firstName} 62 onChangeText={value => this.props.userUpdate({ prop: 'firstName', value })} 63 /> 64 <Input 65 value={this.props.lastName} 66 onChangeText={value => this.props.userUpdate({ prop: 'lastName', value })} 67 /> 68 </View> 69 ); 70 } 71} 72 73const mapStateToProps = (state) => { 74 const { firstName, lastName } = state.userForm; 75 76 return { firstName, lastName }; 77}; 78 79export default connect(mapStateToProps, { 80 userUpdate, userCreate 81})(UserCreate);
ここでいうUserCreate.jsのonErrorを動かしたいです。
どうぞよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/29 02:27
2018/06/29 02:30
2018/06/29 02:34
2018/06/29 09:14
2018/06/29 09:26
2018/06/30 08:18 編集
2018/06/30 02:36
2018/06/30 04:10 編集
2018/06/30 08:26
2018/06/30 08:26
2018/07/01 01:18
2018/07/01 01:35
2018/07/01 14:16
2018/07/03 14:10
2018/07/04 11:25
2018/07/05 03:26