0

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 ?

abu abu
  • 6,599
  • 19
  • 74
  • 131

1 Answers1

1

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.

Ol' Reliable
  • 570
  • 1
  • 8
  • 19