【问题标题】:React Redux Container - Creating a page that includes a redux form and getting an errorReact Redux Container - 创建一个包含 redux 表单的页面并收到错误
【发布时间】:2017-04-27 21:04:40
【问题描述】:

我正在我的 React/Redux 项目中创建一个容器,并且在获得要渲染的基本组件之后,我现在尝试添加一个只需一个输入即可启动的 redux 表单。我相信我已经完成了从 reduxForm 到我的组件的所有正确连接,但无论出于何种原因,我都会收到以下错误:

warning.js:36 Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of `ManageUsersPage`.

invariant.js:44 Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `ManageUsersPage`.

谁能帮我弄清楚我做错了什么?这是我的容器代码:

import React from 'react';
import { connect } from 'react-redux';
import selectManageUsersPage from './selectors';
import styles from './styles.css';
import { Dialog, TextField } from 'redux-form-material-ui';
import { Field, FieldArray, reduxForm, formValueSelector } from 'redux-form/immutable';
import RaisedButton from 'material-ui/RaisedButton';
import { fetchUsers } from './actions';

export class ManageUsersPage extends React.Component { // eslint-disable-line react/prefer-stateless-function

  constructor(props) {
    super(props);

    this.state = {
      userModalOpen: false,
      modalTitle: 'Add a user'
    }

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

  componentWillMount() {
    this.props.dispatch(fetchUsers());
  }


  /**
   * Open the user modal
   */
  openEditUserModal() {
    this.setState({
      userModalOpen: true,
      modalTitle: 'Edit user'
    });
  }


  /**
   *  Close the modal
   */
  handleClose() {
    this.setState({ userModalOpen: false });
  }


  /**
   *  Submit the user form
   */
  submit(formProps) {
    const user = {
      first_name: formProps.get('first_name')
    };

    console.log('user', user);
  }


  render() {
    // Redux Form Props.
    const { handleSubmit, pristine, reset, submitting } = this.props;
    return (
      <div className={styles.manageUsersPage}>
        <button onClick={::this.openCreateUserModal}>Create a user</button>
        <table className="responsive-table">
          <thead>
            <tr>
              <th scope="col">Name</th>
              <th scope="col">Role</th>
              <th scope="col"></th>

            </tr>
          </thead>
          <tbody>
            {this.props.users.map((user, index) => {
              return (
                <tr key={index}>
                  <th scope="row">
                    {user.profile.firstName} {user.profile.lastName}
                  </th>
                  <td>{user.role}</td>
                  <td><button onClick={::this.openEditUserModal}>Edit</button></td>
                </tr>
              )
            })}
          </tbody>
        </table>


        <Dialog
          title={this.state.modalTitle}
          modal={false}
          open={this.state.userModalOpen}
          onRequestClose={this.handleClose}
        >
          <form>
           <div className={styles.form__row}>
             <Field
               name="account_name"
               component={TextField}
               hintText="First Name"
             />
           </div>
           <div className="page-form__block">
             <div className="submit__block">
               <RaisedButton label="Create work order" disabled={pristine || submitting} onClick={handleSubmit(::this.submit)} primary={true} />
             </div>
           </div>
          </form>
        </Dialog>
      </div>
    );
  }
}

const mapStateToProps = selectManageUsersPage();

function mapDispatchToProps(dispatch) {
  return {
    dispatch,
  };
}

//export default connect(mapStateToProps, mapDispatchToProps)(ManageUsersPage);

export default reduxForm({
  form: 'manageUsersPage',
})(connect(mapStateToProps, mapDispatchToProps)(ManageUsersPage));

提前致谢!

【问题讨论】:

  • 你能把console.log('user', user);的结果贴出来,还有用户的道具吗?
  • 我完全愿意,但由于此错误,组件不会加载。如果我的导出语句看起来像 export default connect(mapStateToProps, mapDispatchToProps)(ManageUsersPage); 很好,但是当我尝试使用 reduxForm 时,它给了我这个错误:/

标签: reactjs redux react-redux redux-form


【解决方案1】:

//试试它可能会起作用!

const withConnect = connect( mapStateToProps, mapDispatchToProps, );

导出默认撰写( 与连接, )(withRouter(ManageUsersPage));

【讨论】:

  • 欢迎来到 Stack Overflow!请不要只用源代码回答。尝试对您的解决方案如何工作提供一个很好的描述。请参阅:How do I write a good answer?。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多