首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何禁用redux窗体中的“提交”按钮

如何禁用redux窗体中的“提交”按钮
EN

Stack Overflow用户
提问于 2020-04-10 10:09:54
回答 2查看 479关注 0票数 0

我正在使用redux表单的登录页面上工作。我想禁用提交按钮,直到电子邮件和密码被填充。我试过了,但我失败了,有人能帮我实现我的目标吗?谢谢

代码语言:javascript
复制
<form onSubmit={handleSubmit}>
      <div className="sign-up-form">
        <div className="space-2">
          <Field
            name="email"
            component={renderField}
            type="email"
            label="Email"
          />
        </div>
        <div className="space-2">
          <Field
            name="password"
            component={renderField}
            type="password"
            label="Password"
          />
        </div>
        {/* <button className='login-button' type='submit'>Login</button> */}
        <div className="">
          <button className="login-button" type="submit">
            {loading ? (
              <Loader
                type="ThreeDots"
                color="#ffffff"
                height="10"
                width="100"
              />
            ) : (
              "Login"
            )}
          </button>
        </div>
      </div>
    </form>
EN

回答 2

Stack Overflow用户

发布于 2020-04-10 10:43:54

您可以检查这个链接handleSubmit和道具:

https://redux-form.com/6.0.0-alpha.4/docs/api/props.md/

代码语言:javascript
复制
    const {invalid} = this.props

    return(
    <button type="submit" className="send-btn"
         disabled={invalid|| submitting || pristine}>
         submit
     </button>)
票数 0
EN

Stack Overflow用户

发布于 2020-04-11 08:13:22

一种可能的方法是使用redux表单选择器读取输入值,并返回一个属性,指示是否应该启用按钮。

为此,您需要将表单连接到redux状态,并使用mapStateToProps返回所需的值。

想法:

代码语言:javascript
复制
import { connect } from "react-redux";
import { Field, reduxForm, formValueSelector } from "redux-form";


let MyForm = props => {
  const { enableSubmit } = props; // new property set from redux state

  return (
    <form>
     ... your form 
   </form>
}

const selector = formValueSelector("myForm"); // <-- same as form name
MyForm = connect(state => {
  const hasUsername = selector(state, "email"); // read username value
  const hasPassword = selector(state, "password"); // read username value
  const enableSubmit = hasUsername && hasPassword; // logic for enabling the submit button
  return {
    enableSubmit // this will set property `enableSubmit` which you can read in your component
  };
})(MyForm);

我准备了一个工作示例这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61138056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档