Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass props to the onSubmitSuccess callback in Redux Form?

I am buildng a react native app and am using Redux-form for my registration form. I currently have a form and on success, I would like to navigate to the next page only on success though. In my code, I am using redux-form's onSubmitSuccess function and I need to use props to navigate to the next page, but when I try using props in onSubmitSuccess it tells me that "props is not defined". In redux-form v6.5.0, you can pass props to onSubmitSuccess but I'm unsure of exactly how to do this. This is the documentation for it: https://github.com/erikras/redux-form/blob/master/docs/api/ReduxForm.md#onsubmitsuccess--function-optional

Below is a snippet of my code:

render() {
      const {handleSubmit, touched, error} = this.props

      const onSubmit = (values, dispatch) => {
        this.props.addFirstName(values.firstName)
        this.props.addLastName(values.lastName)
        this.props.addEmailAddress(values.emailAddress)
        this.props.addPassword(values.password)
      }

      return (
          <Container>
                  <View theme={theme}>
                      <Image source={require('../../../images/BG-signUp.png')} style={styles.background} >
                          <Content padder scrollEnabled={false}>
                              <Text style={styles.signupHeader}>
                                  CREATE ACCOUNT
                              </Text>
                              <View style={styles.signupContainer}>
                                <Field name = "firstName" component = {renderFirstName} />
                                <Field name = "lastName" component = {renderLastName} />
                                <Field name = "emailAddress" component = {renderEmailAddress} />
                                <Field name = "password" component = {renderPassword} />
                                <Field name = "confirmPassword" component = {renderConfirmPassword} />
                                <Button
                                    rounded transparent  block
                                    onPress={handleSubmit(onSubmit)}
                                    style={styles.signupBtn}
                                >
                                    Continue
                                </Button>

                                <TouchableOpacity>
                                    <Text style={styles.termsText}>Terms & Conditions</Text>
                                </TouchableOpacity>
                              </View>
                          </Content>
                      </Image>
                  </View>
          </Container>
      )
    }

function bindAction(dispatch) {
    return {
        addFirstName: (firstName) => dispatch(addFirstName(firstName)),
        addLastName: (lastName) => dispatch(addLastName(lastName)),
        addEmailAddress: (emailAddress) => dispatch(addEmailAddress(emailAddress)),
        addPassword: (password) => dispatch(addPassword(password)),
        reset: key => dispatch(reset([{ key: 'orgPage' }], key, 0)),
    };
}

const mapStateToProps = state => ({
  navigation: state.cardNavigation,
  credentials: state.credentials
});

/*export default connect(mapStateToProps, bindAction)(SignUp);*/
SignUp = reduxForm({
  form: 'signup',
  validate,
  onSubmitSuccess: () => {
    this.props.reset(this.props.navigation.key)
  }
})(SignUp);

SignUp = connect(mapStateToProps, bindAction)(SignUp);
like image 411
kjwade3 Avatar asked Jan 05 '23 12:01

kjwade3


1 Answers

SignUp = reduxForm({
  form: 'signup',
  validate,
  onSubmitSuccess: () => {
    this.props.reset(this.props.navigation.key)
  }
})(SignUp);

this has no meaning inside of this fat-arrow function. The props are given to onSubmitSuccess as the third parameter.

SignUp = reduxForm({
  form: 'signup',
  validate,
  onSubmitSuccess: (result, dispatch, props) => {
    props.reset(props.navigation.key) // <---------
  }
})(SignUp);

Hope that helps.

like image 144
Erik R. Avatar answered Jan 07 '23 10:01

Erik R.