react自动触发表单提交_reactjs – redux-form:在输入onChange后触发Form onSubmit

我在redux-form中有一组简单的单选按钮.我需要单选按钮组的onChange事件来触发表单的onSubmit事件.

我正在使用redux-form v5.3.1

建立

RadioFormContainer.js

class RadioFormContainer extends Component {

render() {

const {submit} = this.props;

return (

)

}

}

const mapDispatchToProps = (dispatch) => {

return {

submit: function(values,dispatch) {

// API call after validation

}

}

};

RadioForm.js

class RadioForm extends Component {

render() {

const {

submit,// custom post-validation handler

// via redux form decorator:

handleSubmit,submitting

} = this.props;

return (

checked={radioField.value == "one"}

type="radio"

value="one"

disabled={submitting} />

checked={radioField.value == "two"}

type="radio"

value="two"

disabled={submitting} />

);

}

}

尝试实现,不工作

1.从RadioForm上的componentWillReceiveProps调用handleSubmit

kyleboyle的proposal here根本不起作用.当我从componentWillReceiveProps调用nextProps.handleSubmit时,我得到this familiar error,Uncaught错误:你必须传递handleSubmit()一个onSubmit函数或传递onSubmit作为prop

componentWillReceiveProps(nextProps) {

if (nextProps.dirty && nextProps.valid) {

let doubleDirty = false;

Object.keys(nextProps.fields).forEach(key => {

if (nextProps.fields[key].value !== this.props.fields[key].value) {

doubleDirty = true;

}

});

if (doubleDirty) {

nextProps.handleSubmit();

}

}

}

2.从onChange处理程序调用RadioForm上的提交

erikras proposes this here.但它对我不起作用,因为它会跳过验证.

onChange={(event) => {

radioField.handleChange(event); // update redux state

this.submit({ myField: event.target.value });

}} />

我认为Bitaru(same thread)在他的回复中试图说同样的话,但我不确定.为我调用onSubmit会导致异常Uncaught TypeError:_this2.props.onSubmit不是函数

3.通过this.refs调用表单上的提交

这导致表单实际提交,完全跳过redux-form

onChange={(event) => {

radioField.onChange(event);

this.refs.radioForm.submit();

}} />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值