0

I have this code in my Login.js :

<div className="row justify-content-start">
                <form className="form-signin">
                    <PageDetailsHeading name="Please Log-In to continue"/>
                    <EmailField/>
                    <PasswordField/>
                    <ButtonActor name="Log In" isPrimaryButton="true"/>
                </form>
            </div>

How can I get value of EmailField when I press ButtonActor ?

Here is my EmailField.js:

<div className="form-label-group">
                <input type="email" id="inputEmail" className="form-control" placeholder="Email address" required autoFocus onKeyDown={this._handleKeyDown}/>
                <label htmlFor="inputEmail">Email address</label>
            </div>
3
  • in which component are you storing your state? Commented Jul 29, 2019 at 5:02
  • in EmailField Commented Jul 29, 2019 at 5:05
  • 1
    lift your state up in parent component, in your case in Login.js Commented Jul 29, 2019 at 5:08

2 Answers 2

1

You need to maintain state in Login component only,

state={email:''}

You also need to have a onChange handler and onClick in Login component only and pass them to EmailField and ButtonActor respectively,

onEmailChange = (e) =>{
  this.setState({[e.target.name]:e.target.value})
}

And

onButtonClick = () =>{
   console.log(this.state.email)
}

Pass the function to component,

<EmailField onEmailChange={this.onEmailChange}/>
<ButtonActor name="Log In" isPrimaryButton="true" onButtonClick={this.onButtonClick}/>

EmailField component,

<div className="form-label-group">
   <input type="email" id="inputEmail" className="form-control" placeholder="Email address" required autoFocus onChange={this.props.onEmailChange} name="email"/> //notice attribute `name`
   <label htmlFor="inputEmail">Email address</label>
</div>

ButtonActor component,

<button onClick={this.props.onButtonClick}>Log In</button>
Sign up to request clarification or add additional context in comments.

3 Comments

Btw, is there anyway to retrieve parent component's value ??
Which value you are talking about?
Sorry... I was thinking too much complex, when it can be solved easily ...
0

You can provide that through mentioning that in the state of the component.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.