从回调

时间:2016-08-26 19:00:57

标签: reactjs redux-form

我正在尝试这样做:

"redux-form": "6.0.0-rc.5", 
"react": "^15.3.1",
"react-dom": "^15.3.1",




addressUpdated(newAddress) {
    //TODO, tell Redux form that a value is now available!
    this.props.fields.address.onChange(newAddress.label);
}

地址是一个隐藏字段,一旦调用addressUpdated就应该获取值。 我收到错误

Uncaught TypeError: Cannot read property 'onChange' of undefined

生成组件:

<Field id="address" name="address" type="hidden" component={fieldFactory} />



const fieldFactory = ({id, input, label, type, meta: { touched, error } }) => {
    if(type.match(/hidden/)){
        return(
            <div>
                <input id={id} {...input} type={type} />
                {touched && error && <span>{error}</span>}
        </div>
    );
  }
}

```

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你就不能再在v6的道具中使用 fields

考虑row-form.js:

Row.propTypes = {
  change: PropTypes.func.isRequired, // this binds redux-form change action creator
  dispatch: PropTypes.func.isRequired
}

然后你的回调函数看起来像这样:

addressUpdated(newAddress) {
    this.props.dispatch(this.props.change('address', newAddress.label);
}

请注意,在v6 rc5中,由于bug,这不起作用,但在6.0.1中已修复。

为了让它在rc5中运行,您应该导入全局更改操作创建者并使用您的表单名称发送它:

import { change } from 'redux-form';

Row.propTypes = {
  form: PropTypes.string.isRequired,
  dispatch: PropTypes.func.isRequired
}

addressUpdated(newAddress) {
   this.props.dispatch(change(this.props.form, 'address', newAddress.label);
}

API参考:http://redux-form.com/6.0.1/docs/api/Props.md/