简体   繁体   English

Field Redux表单中的道具组件不起作用

[英]Props component in Field redux-form don't works

I have a simple component for rendering an input 我有一个用于呈现input的简单组件

import React, { Component } from 'react';
import PropTypes from 'prop-types';

class TextInput extends Component {
  constructor(props) {
    super(props);

    this.state = {};
  }

  render() {
    const { placeholder } = this.props;
    return (
      <div>
        <input
          type="input"
          placeholder={placeholder}
        />
      </div>
    );
  }
}

TextInput.propTypes = {
  meta: PropTypes.shape({}),
  placeholder: PropTypes.string
};

export default TextInput;

And I have a form who render my input with Field component from redux-form 我有一个表单,该表单使用redux-form Field组件呈现输入

Like that : 像那样 :

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
  Field,
  reduxForm
} from 'redux-form';

import TextInput from '../../Fields/TextInput/index';

import {
  USERNAME_NAME_FIELD,
  PASSWORD_NAME_FIELD
} from '../../../constants/strings';

class LoginPage extends Component {
  constructor(props) {
    super(props);

    this.state = {};

    this.showResults = this.showResults.bind(this);
  }

  showResults(values) {
    console.log(this);
    console.log(values);
  }

  render() {
    const { handleSubmit } = this.props;
    return (
      <div className="loginPage">
        <form onSubmit={handleSubmit(this.showResults)} className="loginPage__form">
          <div className="loginPage__form__fields">
            <Field
              name={USERNAME_NAME_FIELD}
              type="text"
              component={TextInput}
              label={USERNAME_NAME_FIELD}
            />
          </div>
          <div className="loginPage__form__fields">
            <Field
              name={PASSWORD_NAME_FIELD}
              type="text"
              component={TextInput}
              label={USERNAME_NAME_FIELD}
            />
          </div>
          <div className="loginPage__form__fields">
            <button type="submit">Submit</button>
          </div>
        </form>
      </div>
    );
  }
}

LoginPage.propTypes = {
  values: PropTypes.shape({}),
  handleSubmit: PropTypes.func
};


export default reduxForm({
  form: 'loginPage' // a unique identifier for this form
})(LoginPage);

And I can't get the values when I passed my own component, but if I remove my own component and I replace it with a input string, he works and I can get my values. 而且,当我传递自己的组件时,我无法获取值,但是如果删除自己的组件,并用input字符串替换它,则他可以工作,并且我可以获取值。

In your sample code, you're not passing placeholder prop. 在示例代码中,您没有传递placeholder prop。 But then, you're asking about value . 但是,那是您在询问value Assuming you're asking about how to pass value and other HTML attributes such as placeholder , the answer can be found in their docs . 假设您要询问如何传递value和其他HTML属性(如placeholder ,答案可以在他们的文档中找到。

Any custom props passed to Field will be merged into the props object on the same level as the input and meta objects. 传递给Field的所有自定义道具都将在与输入和元对象相同的级别上合并到道具对象中。

value is part of input object, and other HTML attributes such as placeholder are at same level as input . valueinput对象的一部分,其他HTML属性(如placeholderinput处于同一级别。 I know it's confusing as hell and it took me a long while to wrap my head around it. 我知道这真是令人迷惑,我花了很长时间才把头缠住它。

So to get the value, you would use: 因此,要获得价值,您可以使用:

const { placeholder, input: { value } } = this.props;

And to pass placeholder : 并通过placeholder

<Field
  name={USERNAME_NAME_FIELD}
  type="text"
  component={TextInput}
  label={USERNAME_NAME_FIELD}
  placeholder="some placeholder text"
/>

Also, your input type is not a valid type. 另外,您的输入type也不是有效的类型。 It should be type="text" (judging from your code). 它应该是type="text" (根据您的代码判断)。 You can see a list of valid input types here . 您可以在此处看到有效输入类型的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM