从回调更新 redux 表单字段
Update redux form field from a callback
我正在尝试这样做:
"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);
}
address 是一个隐藏字段,一旦调用 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>
);
}
}
```
有什么想法吗?
如果我理解正确,你不能再在 v6.
的 props 中使用 fields
考虑行-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);
}
请注意,由于 bug,在 v6 rc5 中这不起作用,但它已在 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);
}
我正在尝试这样做:
"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);
}
address 是一个隐藏字段,一旦调用 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>
);
}
}
```
有什么想法吗?
如果我理解正确,你不能再在 v6.
的 props 中使用 fields考虑行-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);
}
请注意,由于 bug,在 v6 rc5 中这不起作用,但它已在 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);
}