如何在反应中获取子组件的状态?

时间:2017-02-20 23:06:54

标签: javascript reactjs react-jsx

我有Login ComponentInput Components

class Login extends React.Component {

constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleChangeInput = this.handleChangeInput.bind(this);

    this.state = {
        email_user: "LOL",
        password_user: "",
    };
}

handleSubmit(e) {
    //alert('A name was submitted: ');
    e.preventDefault();
    console.log(this.state);
}

handleChangeInput(e){
    console.log(e.target.value);
    this.setState({value: e.target.value});
}

render() {

    return (
       <Input size="L" type="email" name="email_user" placeholder="Email" value={this.state.email_user} />
       <Input size="L" type="password" name="password_user" placeholder="Mot de passe" value={this.state.password_user} onChange={this.handleChangeInput} />        );
    }

}

我的输入组件返回一些HTML,如:

            <div className={classInput}>
                <span className="input__text">
                    <input type={this.props.type} name={this.props.name} placeholder={this.props.placeholder} defaultValue={this.props.value} onChange={this.handleChange} required />
                </span>
            </div>

我的输入statehandleChange内的Input Component更新。

state无法看到这些Login Component。无论有没有handleChangeInputLogin Component中的状态都不会被保存。

Login Component如何访问子组件内的更新state

0 个答案:

没有答案