Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为什么pristine props不为两个Redux字段禁用按钮_Reactjs_Redux_React Redux_Submit_Redux Form - Fatal编程技术网

Reactjs 为什么pristine props不为两个Redux字段禁用按钮

Reactjs 为什么pristine props不为两个Redux字段禁用按钮,reactjs,redux,react-redux,submit,redux-form,Reactjs,Redux,React Redux,Submit,Redux Form,我有两个字段和一个提交按钮。这两个字段由redux表单控制。在按下submit按钮之前,这两个字段都需要填充,因此如果这两个字段为空,我将使用pristine和Submiting道具禁用按钮。 问题是,如果我填充了这两个字段中的一个,按钮将变为启用状态,但我希望在填充这两个字段后按钮将被启用。 我做错了什么 <form onSubmit={handleSubmit(this.submit)}> <div id="product-searchbar"> <Fie

我有两个字段和一个提交按钮。这两个字段由redux表单控制。在按下submit按钮之前,这两个字段都需要填充,因此如果这两个字段为空,我将使用pristine和Submiting道具禁用按钮。 问题是,如果我填充了这两个字段中的一个,按钮将变为启用状态,但我希望在填充这两个字段后按钮将被启用。 我做错了什么

<form onSubmit={handleSubmit(this.submit)}>
 <div id="product-searchbar">
  <Field
   component="TextField"
   type="text"
   name="productId"
   placeholder="Enter a name product..."
  />
 </div>
 <Field
  component="Dropdown"
  header="Choose a product"
  name="type"
  items={[
   { value: "product a", id: 1 },
   { value: "product b", id: 2 }
  ]}
 />
 <button
  type="submit"
  disabled={pristine || submitting}
 >
  <SaveIcon />
 </button>
</form>

let ComponentContainer= reduxForm({
 form: "formProduct"
})(ComponentWrapped);

让ComponentContainer=reduxForm({
表格:“formProduct”
})(组件包装);

pristine
表示“没有字段”的值属性与其初始属性不同,因此一旦一个字段的值与您初始化的值不同,
pristine
将设置为
false
。您可以使用此变量启用
reset()
功能

您要做的是将这两个字段设置为必填字段。有两种方法: 您可以根据需要设置这两个字段,如下所示:

<Field
   component="TextField"
   type="text"
   name="authId"
   placeholder="Enter an auth-Id..."
   required
/>

表单的值是从状态值初始化的吗?()因为
pristine
仅在这种情况下使用。如果不是这样,您应该将这些字段设置为
required
@SamHecquet是的,它们是从状态值初始化的。我更新了代码非常感谢您的帮助。
const required = value => (value ? undefined : 'Required');

render() {
  return (
    <form onSubmit={handleSubmit(this.submit)}>
      <div id="authId-searchbar">
        <Field
          component="TextField"
          type="text"
          name="authId"
          placeholder="Enter an auth-Id..."
          validate={required}
        />
      </div>
      <Field
        component="Dropdown"
        header="Choose a credential type"
        name="type"
        items={[
          { value: "product a", id: 1 },
          { value: "product b", id: 2 }
        ]}
        validate={required}
      />
      <button
        type="submit"
        disabled={pristine || submitting}
      >
        <SaveIcon />
      </button>
    </form>
  );
}