【问题标题】:Migrating redux-form to v6, onBlur and onChange functions将 redux-form 迁移到 v6、onBlur 和 onChange 函数
【发布时间】:2016-11-25 03:44:42
【问题描述】:

我正在将 redux-form 迁移到最新版本 6.0.0rc3。在这个版本中,'fields' 数组消失了,取而代之的是一个 Field 组件(参见http://redux-form.com/6.0.0-rc.3/docs/MigrationGuide.md/)。我曾经像这样扩展 v5 中的默认 onBlur 函数:

const { user_zipcode } = this.props.fields;
onChange={
  val => user_zipcode.onChange(this._handleZipcodeChange(val))
}

但是,在新版本中,这不能再这样做了,因为this.props.fields 不存在。

根据我在文档中找到的内容,新方法应该是为每个具有不同功能的表单字段创建一个组件,并在那里扩展 onBlur 功能:

_onBlur() {
  // my custom blur-function
}

render() {
  const { input, touched, error } = this.props;
  return (
    <input
      className={styles.input}
      {...input}
      onBlur={() => {
        // 2 functions need to be called here
        this._onBlur();
        input.onBlur();
      }}
    />
  );
}

这很好,除了我需要为每个需要在模糊事件发生时执行不同操作的字段创建一个新元素。在某些领域,我必须调用一个 API,我通过调度一个动作来做到这一点。例如,我必须这样做才能获得地址。在这些组件中,我必须连接我的商店,所以它会连接很多次。

我试图将我的自定义函数从父组件传递给 Field 组件,如下所示:

<Field
  type="text"
  name="user_zipcode"
  component={Zipcode}
  onBlurFunction={this._myBlurFunction}
/>

并从道具中获取我的组件中的两个功能:

...
onBlur={() => {
  input.onBlurFunction();
  input.onBlur();
}}
...

由于新的React warning,这无法正常工作。

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: reactjs redux redux-form


    【解决方案1】:

    我最终为每个具有 onChange 或 onBlur 函数的表单域创建了一个自定义组件。这样你就可以运行这两个函数,所以所有默认的 redux-form 动作都会被调用。

    // Licenceplate-component
    
    export default class Licenceplate extends Component {
    
        _handleBlur = ({ currentTarget: { value } }) => {
            // my blur function
        }
    
        render() {
            const {
              input,
              meta: { touched, error }
            } = this.props;
    
            return (
              <div className={styles.wrapper}>
                <input
                  {...input}
                  type="text"
                  onBlur={
                    e => input.onBlur(this._handleBlur(e))
                  }
                />
    
             </div>
           );
       }
    }
    
    
    // In my form-component
    <Field
        component={Licenceplate}
        name="car_licenceplate"
        type="text"
    />
    

    【讨论】:

    • 如果您使用示例中提到的Field 的默认方式:&lt;Field name="firstName" component="input" type="text" placeholder="First Name"/&gt;,您会怎么做?你也试过这种风格吗?
    • 有人找到了解决方案?
    【解决方案2】:

    在文档中是这样说的

    props : 对象 [可选]

    带有自定义props的对象通过Field组件传递给组件提供给组件props。这个 props 将被合并到 Field 本身提供的 props 中。如果您使用 TypeScript,这可能很有用。这个结构是完全可选的;将 props 传递给组件的主要方法是将它们直接传递给 Field 组件,但是如果出于某种原因,您希望将它们捆绑到一个单独的对象中,则可以通过将它们传递给 props 来实现。

    <Field component={renderField} props={{ disabled: true }} />
    

    http://redux-form.com/6.0.0-rc.3/docs/api/Field.md/

    也许试试那个。它可能会防止错误。或者在它被传递到输入之前拉出你的函数道具。

    【讨论】:

    • 试过了,但我得到了同样的错误。我可以提取我的 functionProp,但我想要一个更通用的解决方案。另一方面,我猜,每个具有特定功能的表单域的组件还不错吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多