在 React Native 中自动提交 Redux 表单

取名

任何人都知道在满足某些条件时如何在React Native中自动提交Redux表单?我在下面的尝试是发出警告。

在文档中有一个用于远程提交的示例,但它使用的是 HTML 表单的<form onSubmit={handleSubmit}>. 在React Native中这相当于什么?

我的尝试:当表单的输入长度 >= 2 时提交

class MyClass extends React.Component {
  constructor(props) {
    super(props);
    this.handlSubmitWrapper = this.handlSubmitWrapper.bind(this);
  }

  handlSubmitWrapper() {
    const { handleSubmit } = this.props;
    handleSubmit(() => console.log('submitting...'))();
  }

  getTextInput({ input: { onChange, value, ...otherProps }, autoSubmit }) {
    if (value && value.length > 1) {
      // triger submit
      autoSubmit();
    }

    return (
      <TextInput
        onChangeText={onChange}
        style={{height: 50, backgroundColor: '#666'}}
        {...otherProps}
        maxLength={2}
      />
    );
  }

  render() {
    return (
      <View>
        <Field name="myText" 
               component={this.getTextInput} 
               autoSubmit={this.handlSubmitWrapper} 
        />
      </View>
    );
  }
}

const MyForm = reduxForm({
  form: 'setupPasscode',
})(MyClass);

export default connect()(MyForm);

警告:

ExceptionsManager.js:71 Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.

文森特·达穆尔

您在渲染组件时调用提交操作。你不能用反应来做到这一点。您应该改为使用 TextInput onChange 方法来实现这一点。

class MyClass extends React.Component {
  constructor(props) {
    super(props);
    this.handlSubmitWrapper = this.handlSubmitWrapper.bind(this);
    this.handleInputChange = this.handleInputChange.bind(this);
  }

  handlSubmitWrapper() {
    const { handleSubmit } = this.props;
    handleSubmit(() => console.log('submitting...'))();
  }

  handleInputChange(event) {
    const newText = event.nativeEvent.text;
    if (newText && newText.length > 1) {
      // triger submit
      this.handlSubmitWrapper();
    }
  }

  getTextInput({ input: { onChange, value, ...otherProps } }) {
    return (
      <TextInput
        onChange={this.handleInputChange}
        onChangeText={onChange}
        style={{height: 50, backgroundColor: '#666'}}
        {...otherProps}
        maxLength={2}
      />
    );
  }

  render() {
    return (
      <View>
        <Field name="myText" component={this.getTextInput} />
      </View>
    );
  }
}

const MyForm = reduxForm({
  form: 'setupPasscode',
})(MyClass);

export default connect()(MyForm);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React + Redux:提交多组件表单

来自分类Dev

React + Redux:提交多组件表单

来自分类Dev

{React Native}提交后重置Formik表单

来自分类Dev

React 中的表单提交

来自分类Dev

什么是...存储在React Native Redux中?

来自分类Dev

React Native 中的 Redux-Saga 错误

来自分类Dev

React、React Redux、Redux 表单类型无效

来自分类Dev

提交react / redux表单时无法在php中检索POST值

来自分类Dev

提交react / redux表单时无法在php中检索POST值

来自分类Dev

React Redux-动态创建 Redux 表单

来自分类Dev

为什么 redux 表单自动提交然后获取请求?

来自分类Dev

React Native + Redux基本认证

来自分类Dev

React Native Redux条件渲染

来自分类Dev

React Native 与 Redux - 意外令牌

来自分类Dev

Redux可以在React Native中存储令牌会话吗?

来自分类Dev

Redux 中的 React-native 嵌套导航器

来自分类Dev

React Native 中的 redux-form 有问题

来自分类Dev

在 React Native Redux 中获取 store not found 错误

来自分类Dev

在 react-native 和 redux 以及 react-native-maps 中处理多请求

来自分类Dev

React-Redux,使用来自子组件的数据提交表单

来自分类Dev

使用 Redux 应用程序在 React 中提交表单后如何正确路由

来自分类Dev

React Redux, Uncaught TypeError: this.props.dispatch is not a function, 当试图调度表单提交时?

来自分类Dev

简历阻止了 React 中的表单提交

来自分类Dev

如何连接React Dropzone和Redux表单

来自分类Dev

React-Redux 创建操作失败,表单

来自分类Dev

如何处理子组件中的redux表单提交?

来自分类Dev

React Native Redux-为什么我不能访问Redux存储中的数据并显示它们?

来自分类Dev

如何将Redux Toolkit的createSlice创建的Redux动作传递给React Native中的屏幕

来自分类Dev

如何提交表单或在带有react-native-testing-library的输入上按Enter键

Related 相关文章

  1. 1

    React + Redux:提交多组件表单

  2. 2

    React + Redux:提交多组件表单

  3. 3

    {React Native}提交后重置Formik表单

  4. 4

    React 中的表单提交

  5. 5

    什么是...存储在React Native Redux中?

  6. 6

    React Native 中的 Redux-Saga 错误

  7. 7

    React、React Redux、Redux 表单类型无效

  8. 8

    提交react / redux表单时无法在php中检索POST值

  9. 9

    提交react / redux表单时无法在php中检索POST值

  10. 10

    React Redux-动态创建 Redux 表单

  11. 11

    为什么 redux 表单自动提交然后获取请求?

  12. 12

    React Native + Redux基本认证

  13. 13

    React Native Redux条件渲染

  14. 14

    React Native 与 Redux - 意外令牌

  15. 15

    Redux可以在React Native中存储令牌会话吗?

  16. 16

    Redux 中的 React-native 嵌套导航器

  17. 17

    React Native 中的 redux-form 有问题

  18. 18

    在 React Native Redux 中获取 store not found 错误

  19. 19

    在 react-native 和 redux 以及 react-native-maps 中处理多请求

  20. 20

    React-Redux,使用来自子组件的数据提交表单

  21. 21

    使用 Redux 应用程序在 React 中提交表单后如何正确路由

  22. 22

    React Redux, Uncaught TypeError: this.props.dispatch is not a function, 当试图调度表单提交时?

  23. 23

    简历阻止了 React 中的表单提交

  24. 24

    如何连接React Dropzone和Redux表单

  25. 25

    React-Redux 创建操作失败,表单

  26. 26

    如何处理子组件中的redux表单提交?

  27. 27

    React Native Redux-为什么我不能访问Redux存储中的数据并显示它们?

  28. 28

    如何将Redux Toolkit的createSlice创建的Redux动作传递给React Native中的屏幕

  29. 29

    如何提交表单或在带有react-native-testing-library的输入上按Enter键

热门标签

归档