简体   繁体   English

在反应中处理来自子组件的提交和更改

[英]Handle submit and change from child component in react

I have two components :我有两个组成部分:

  • LoginForm which is used to render the form to login in the app LoginForm用于呈现表单以在应用程序中登录
  • LoginPage which get the data entered in the LoginForm component and send it to a server LoginPage获取在 LoginForm 组件中输入的数据并将其发送到服务器

For the moment I would like to handle the form submit and the change of an input value.目前我想处理表单提交和输入值的更改。 I read these two articles in the react official website to help me :我在react官方网站上阅读了这两篇文章来帮助我:

But I still don't detect the submit and the change from the LoginPage component when I'm entering a value in LoginForm.但是当我在 LoginForm 中输入一个值时,我仍然没有检测到来自 LoginPage 组件的提交和更改。

Can you help me to see where is my mistake ?你能帮我看看我的错误在哪里吗? Thanks by advance.提前致谢。

My two components :我的两个组成部分:

LoginPage.js登录页面.js

 class LoginPage extends Component {
 constructor(props) {
    super(props);
    this.state = {
        login: true, //switch between Login and SignUp
        email: '',
        password: '',
        firstName: '',
        lastName: ''
    };
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleInputChange = this.handleInputChange.bind(this);
}

handleSubmit(){
    alert("SUBMIT");
}

handleInputChange(event) {
    alert("YOUHOU");
    const target = event.target;
    const value = target.value;
    const name = target.name;

    this.setState({
      [name]: value
    });

    alert("YEEEP");
  }

render(){
    return (
        <div>
            <div>
                {this.state.login ? 
                    <Login onSubmit={this.handleSubmit} onChange={this.handleInputChange}/> 
                    : 
                    <Register />
                }
            </div>
            <a
                onClick={() => this.setState({ login: !this.state.login })}
            >
            {this.state.login ? 'Besoin d\'un compte ?' : 'Déjà un compte ?'}
            </a>
        </div>
    )
}

}

LoginForm.js登录表单.js

class LoginForm extends Component {
render(){
    return (
        <div>
          <Card>
            <form onSubmit={this.props.handleSubmit}>
              <div>
                <div>
                    <TextField name="email" floatingLabelText="Email" errorText="Champ obligatoire" type="text" onChange={this.props.handleInputChange}/>
                </div>
                <div>
                    <TextField name="password" floatingLabelText="Mot de passe" errorText="Champ obligatoire" type="password" onChange={this.props.handleInputChange} />
                </div>
                <CardActions>
                    <div>
                        <RaisedButton label="Se connecter" primary={true} type="submit" fullWidth />
                    </div>
                </CardActions>
              </div>
            </form>
          </Card>
        </div>
    );
}
}

handleInputChange is passed down to LoginForm as onChange prop and similarly handleSubmit is passed down by the name onSubmit and hence you need to use it like handleInputChange被传递给LoginForm作为onChange道具,同样的handleSubmit被传递给onSubmit名称,因此你需要像这样使用它

class LoginForm extends Component {
    render(){
        return (
            <div>
              <Card>
                <form onSubmit={this.props.onSubmit}>
                  <div>
                    <div>
                        <TextField name="email" floatingLabelText="Email" errorText="Champ obligatoire" type="text" onChange={this.props.onChange}/>
                    </div>
                    <div>
                        <TextField name="password" floatingLabelText="Mot de passe" errorText="Champ obligatoire" type="password" onChange={this.props.onChange} />
                    </div>
                    <CardActions>
                        <div>
                            <RaisedButton label="Se connecter" primary={true} type="submit" fullWidth />
                        </div>
                    </CardActions>
                  </div>
                </form>
              </Card>
            </div>
        );
    }
}

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

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