无法在React Web App中添加电子邮件字段

时间:2018-11-29 22:12:25

标签: reactjs forms mobx

我正在开发一个与Mobx集成的小型React App。我已经建立了一个会员商店,如下:

import {observable, action, computed} from 'mobx';

 class MemberStore {
@observable members = [];

@action addMember(data) {
    const existing = this.members;
    this.members = existing.concat(data);
}

@computed get memberCount() {
    return this.members.length;
 }

}
const store = new MemberStore();
export default store;

现在我有一个联系人页面,其中包含姓名和电子邮件形式。现在,我可以添加名称,但不能添加电子邮件。因此,基本上,当我单击“提交”按钮时,我只会看到名称,并在名称旁边得到一个0。

import React, { Component } from 'react';
import Navbar from '../components/Navbar';
import Footer from '../components/Footer';
import Jumbotron from '../components/Jumbotron';
import {inject, observer} from 'mobx-react';

@inject('MemberStore')
@observer

class Contact extends Component {
 handleSubmit = (e) => {
    e.preventDefault();

    const member = this.member.value;
    const email = this.email.value;
    // const email = this.email.value;
    this.props.MemberStore.addMember(member, email);
    this.member.value = '';
    this.email.value = '';
}
render() {
    const {MemberStore} = this.props;

    return (
        <div>
            <Navbar />
            <Jumbotron title="Contact Page" subtitle="You want to get in touch"/>
            <div className="container">
               <h2>You have {MemberStore.memberCount} members.</h2>

               <form onSubmit={e => this.handleSubmit(e)}>
                   <input type="text" placeholder="Enter Your Name" ref={input => this.member = input }/>
                   <div>
                   <input type="text" placeholder= "Enter Your Email" ref={input => this.email = input }/>
                   </div>
                   <button>Submit</button>
               </form>
            <ul>
                {MemberStore.members.map((member,email) => (
                    <li key={member}>
                        {member}
                        {email}

                    </li>
                ))}
            </ul>
            </div>
            <Footer />
        </div>
    )
  }
 }

 export default Contact;

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的问题可能是addMember带有一个参数data,而您在调用它时将两个参数传递给它:addMember(name, email)

您应该将名称和电子邮件作为对象传递:

addMember({name, email})

MemberStore.members数组映射到JSX的代码中,当它只应将一个对象作为参数时,也将使用两个参数:

{MemberStore.members.map({member,email}) => (

请注意,我正在使用上面的ES6 Object Literal Property Value Shorthand语法。