我正在构建一个 React Native 应用程序,并且正在使用 Redux-form 作为我的注册表单。我目前有一个表格,如果成功,我只想在成功时导航到下一页。在我的代码中,我使用的是 redux-form 的 onSubmitSuccess 函数,我需要使用 props 导航到下一页,但是当我尝试在 onSubmitSuccess 中使用 props 时,它告诉我“props 未定义”。在 redux-form v6.5.0 中,您可以将 props 传递给 onSubmitSuccess 但我不确定如何做到这一点。这是它的文档:https://github.com/erikras/redux-form/blob/master/docs/api/ReduxForm.md#onsubmitsuccess--function-optional

下面是我的代码片段:

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);

最佳答案

SignUp = reduxForm({
  form: 'signup',
  validate,
  onSubmitSuccess: () => {
    this.props.reset(this.props.navigation.key)
  }
})(SignUp);
this 在这个粗箭头函数中没有意义。 props 作为第三个参数提供给 onSubmitSuccess
SignUp = reduxForm({
  form: 'signup',
  validate,
  onSubmitSuccess: (result, dispatch, props) => {
    props.reset(props.navigation.key) // <---------
  }
})(SignUp);

希望有帮助。

关于javascript - 如何将 Prop 传递给 Redux 表单中的 onSubmitSuccess 回调?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42356471/

10-10 20:11