Reputation: 7052
I am new in react.js. I am trying to collect values from a Form. I read several SO question and article found from Google search. I am confused now. Which one is the actual way of collecting values from a Form in react.js ? I read this SO question. I found here
<input type="text" name="email" placeholder="Email" value={this.state.email} onChange={this.handleEmailChange} />
I read this article and found below code
<input type="text" name="fname" value={fname} onChange={this.onChange} />
In a Facebook group, members advised me to use this.
Which way should I follow ? Should I use state
?
Upvotes: 0
Views: 245
Reputation: 570
For a simple form, you don't need to use Formik.
<input type="text" name="email" placeholder="Email" value={this.state.email} onChange={this.handleEmailChange} />
is the correct way to do it.
In response to
how can I catch the value inside a function
You need to create a method in your class
handleEmailChange(e) {
let val = e.target.value;
}
val
contains the value of the input.
Or even easier is to just get it from this.state.email
.
Upvotes: 1