abu abu
abu abu

Reputation: 7052

How to collect values from a form in react.js?

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

Answers (1)

Ol&#39; Reliable
Ol&#39; Reliable

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

Related Questions