将变量传递给React组件会产生"未知属性错误"

时间:2016-10-30 06:44:37

标签: javascript reactjs

超级简单还是我想的!只是尝试将字符串传递给另一个组件,但它没有识别该属性,因为它正在使用字符串插值。

修改

我认为它可能与升级redux-form有关,后者改变了输入选择器。我使用React 15.3.1,我相信他们改变了影响Redux的输入工作方式。 (https://github.com/erikras/redux-form/issues/1249)。

import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../../actions';
import { reduxForm } from 'redux-form';

import errorListItem from '../error-list-item/index.js';

class Signup extends Component {
  handleFormSubmit(formProps) {
    this.props.signupUser(formProps);
  }

  renderAlert() {
    const errorObject = this.props.errorMessage;

    // single (connection/api) error
    if (errorObject === 'Network Error') {
      return (
        <div className="alert alert-danger">
          <errorListItem message="hello" />
        </div>
      );

    // error object from BE
    } else if (errorObject && typeof errorObject === 'object') {
      const keys = Object.keys(errorObject);

      debugger;
      if (keys.length === 0) return;

      return Object.keys(errorObject).map(field => {
        return errorObject[field].map(problem => {
          const message = `${field} ${field}`;

          return (
           <ul className="error-message-list">
              <errorListItem message={message} />
            </ul>
           );
        });
      });
    }
  }

  render() {
    const { handleSubmit, fields: { email, password, passwordConfirm, username }} = this.props;

    return (
      <div className="signup">
        <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
          <fieldset className="form-group">
            <label>Username:</label>
            <input className="form-control" {...username} />
            {username.touched && username.error && <div className="error">{username.error}</div>}
          </fieldset>
          <fieldset className="form-group">
            <label>Email:</label>
            <input className="form-control" {...email} />
            {email.touched && email.error && <div className="error">{email.error}</div>}
          </fieldset>
          <fieldset className="form-group">
            <label>Password:</label>
            <input type="password" className="form-control" {...password} />
            {password.touched && password.error && <div className="error">{password.error}</div>}
          </fieldset>
          <fieldset className="form-group">
            <label>Confirm Password:</label>
            <input className="form-control" {...passwordConfirm} />
            {passwordConfirm.touched && passwordConfirm.error && <div className="error">{passwordConfirm.error}</div>}
          </fieldset>
          <button action="submit" className="btn btn-primary">Sign up</button>
        </form>
        {this.renderAlert()}
      </div>
    );
  }
}

function validate(formProps) {
  const errors = {};

  if (!formProps.email) {
    errors.email = 'Please enter an email';
  }

  if (!formProps.password) {
    errors.password = 'Please enter a password';
  }

  if (!formProps.passwordConfirm) {
    errors.passwordConfirm = 'Please enter a password confirmation';
  }

  if (!formProps.username) {
    errors.username = 'Please enter a username';
  }

  // Passwords match
  if (formProps.password !== formProps.passwordConfirm) {
    errors.password = 'Passwords must match';
  }

  return errors;
}

function mapStateToProps(state) {
  return { errorMessage: state.auth.errors };
}

export default reduxForm({
  form: 'signup',
  fields: ['email', 'password', 'passwordConfirm', 'username'],
  validate
}, mapStateToProps, actions)(Signup);

子组件:

import React from 'react';

require('./styles.scss');

const errorListItem = ({ message }) => {
  return (<li>{message}</li>)
}

export default errorListItem;

尝试的解决方案:

  1. 在不使用变量

    的情况下传递属性
    <ul className="error-message-list">
      <errorListItem message={`${field}  ${problem}`} />
    </ul>
    
  2. 使用ES6解构传递属性

    <ul className="error-message-list">
      <errorListItem message />
    </ul>
    
  3. 使用较旧的JS语法传递属性而不使用``。

      const message = "" + field + " " +  problem + "";
    
      return (
        <ul className="error-message-list">
          <errorListItem message={message} />
        </ul>
       );
    
  4. 尝试让子组件渲染任何东西!

      const errorListItem = ({ message }) => {
        return <li>Help Me, Obi-Wan Kenobi. You're My Only Hope</li>
      }
    
  5. 咨询的文件:

    https://facebook.github.io/react/warnings/unknown-prop.html

    错误讯息:

    Warning: Unknown prop `message` on <errorListItem> tag. Remove this prop from the element.
    

    预期行为:

    1. 它应该能够将字符串作为属性传递。如果我使用以下内容,它将起作用:

      <ul className="error-message-list">
        <errorListItem message='message' />
      </ul>
      
    2. 如果它没有呈现,则错误消息不应该是&#34;未知属性&#34;而是给出无效的属性值。

    3. 版本:

      "react": "^0.14.3",
      "react-redux": "^4.0.0",
      "react-router": "^2.0.1",
      "redux": "^3.0.4",
      "redux-form": "^5.3.1",
      

1 个答案:

答案 0 :(得分:2)

我认为问题在于您如何命名errorListItem组件。在JSX中,自定义组件名称应以大写字母开头,而普通html以小写字母开头。因此,React认为您正在尝试使用未知属性呈现名为errorListItem的“普通”标记。错误显示,因为React验证传递给html标签的道具:https://facebook.github.io/react/warnings/unknown-prop.html

尝试在导入时重命名无状态组件(并在定义它时也一致):

import ErrorListItem from '../error-list-item/index.js';
// Rest of your code...
<ErrorListItem message={message} />