简体   繁体   English

将redux-form迁移到v6,onBlur和onChange函数

[英]Migrating redux-form to v6, onBlur and onChange functions

I'm migrating redux-form to the newest version, 6.0.0rc3. 我正在将redux-form迁移到最新版本6.0.0rc3。 In this version the 'fields' array is gone, and is replaced by a Field component (see http://redux-form.com/6.0.0-rc.3/docs/MigrationGuide.md/ ). 在这个版本中,'fields'数组已经消失,并被Field组件取代(参见http://redux-form.com/6.0.0-rc.3/docs/MigrationGuide.md/ )。 I used to extend the default onBlur function in v5 like this: 我曾经在v5中扩展默认的onBlur函数,如下所示:

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

However, in the new version this can't be done anymore, because this.props.fields doesn't exist. 但是,在新版本中不能再这样做,因为this.props.fields不存在。

From what I found in the docs, the new way should be to create a component for every form field that has a distinct function, and extend the onBlur function there: 从我在文档中找到的内容来看,新的方法应该是为每个具有不同功能的表单字段创建一个组件,并在那里扩展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();
      }}
    />
  );
}

This is fine, except that I need to create a new element for each field that needs to do something different when the blur event occurs. 这很好,除了我需要为每个字段创建一个新元素,当模糊事件发生时需要做一些不同的事情。 In some fields I have to make a call to an API, which I do by dispatching an action. 在某些领域,我必须调用API,我通过调度操作来完成。 For example, I have to do this to get an address. 例如,我必须这样做才能得到一个地址。 In these components I have to connect my store, so it gets connected many times. 在这些组件中,我必须连接我的商店,因此它连接多次。

I tried to pass my custom function from the parent to the Field component, like this: 我试图将我的自定义函数从父组件传递给Field组件,如下所示:

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

and getting both functions in my component from the props: 从props中获取我的组件中的两个函数:

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

This didn't work correctly because of the new React warning . 由于新的React警告,这无法正常工作。

Is there a better way to do this? 有一个更好的方法吗?

I ended up with creating a custom component for every form-field that has a onChange or onBlur function. 我最终为每个具有onChange或onBlur函数的表单字段创建了一个自定义组件。 In that way you can run both functions, so all the default redux-form actions are called. 通过这种方式,您可以运行这两个函数,因此将调用所有默认的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"
/>

In the docs it says this 在文档中它说明了这一点

props : object [optional] 道具:对象[可选]

Object with custom props to pass through the Field component into a component provided to component prop. 具有自定义道具的对象将Field组件传递到提供给组件prop的组件中。 This props will be merged to props provided by Field itself. 这个道具将合并到Field本身提供的道具。 This may be useful if you are using TypeScript. 如果您使用TypeScript,这可能很有用。 This construct is completely optional; 这个结构是完全可选的; the primary way of passing props along to your component is to give them directly to the Field component, but if, for whatever reason, you prefer to bundle them into a separate object, you may do so by passing them into props. 将props传递给组件的主要方法是将它们直接提供给Field组件,但是如果由于某种原因,您希望将它们捆绑到一个单独的对象中,则可以通过将它们传递给props来实现。

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

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

Maybe try out that one. 也许尝试一下。 It will probably prevent the error. 它可能会防止错误。 Or pull of your function prop before it gets passed down to the input. 或者在函数道具传递到输入之前拉动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM