I don't know if I'm doing this correctly...
If I want to get value from an input I use this.refs.whatever.value.trim() but if that input is a stateless function component how do I do retrieve the value onSubmit?
I know this isn't correct now after researching but how are you supposed to get value from these input fields?
import React, {Component} from 'react'
import {InputField} from '../components/forms/InputField'
import {Button} from '../components/forms/Button'
export default class SignupWrapper extends Component {
_handleSubmit(e) {
e.preventDefault();
const email = this.refs.email.value.trim();
const password = this.refs.password.value.trim();
const confirm = this.refs.confirm.value.trim();
console.log({email, password, confirm});
}
render() {
return (
<form id="application-signup" onSubmit={this._handleSubmit.bind(this)}>
<InputField type={'email'} name={'email'} text={'email'}
helpBlock={'email is required'} ref="email" />
<InputField type={'password'} name={'password'} text={'password'}
helpBlock={'password is required'} ref="password" />
<InputField type={'password'} name={'confirm'} text={'confirm password'}
helpBlock={'password confirmation is required'} ref="confirm" />
<Button type={'submit'} className={'btn btn-primary'} text={'signup'} />
</form>
)
}
}
this is the stateless inputfield
import React from 'react'
export const InputField = (props) => (
<div className="form-group col-xs-12">
<label htmlFor={props.name}>{props.text}</label>
<input type={props.type} name={props.name} className="form-control"
data-stripe={props.stripe} />
<span className="help-block">{props.helpBlock}</span>
</div>
)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…