Академический Документы
Профессиональный Документы
Культура Документы
WORK
ALXMRTNZ ABOUT
THOUGHTS
MAY 2, 2018
When I started learning and building with React, app development, data-binding, and state
management were new to me and the idea of using controlled components to store and
update input eld values seemed like a lot of unnessesary overhead.
Luckily, a coworker suggested I try out Redux Form, a library that helps you manage form
state and validation in redux rather than in a component.
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 1/10
7/14/2018 React + Redux Form | Alex Martinez
Focused eld
If eld values are valid
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 2/10
7/14/2018 React + Redux Form | Alex Martinez
A er se ing up Redux Form, you’ll be able to see it at work using Redux DevTools:
The form is stored under the name you give it, in this case SignInForm , while input data is
stored under values and errors under syncErrors . Redux Form then provides meta
information like those error messages, whether an input has been touched , and whether
the form is or isn’t dirty as props in your form and input components.
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 3/10
7/14/2018 React + Redux Form | Alex Martinez
The following is an example form for a Sign In screen. reduxForm() connects the form
component to redux while redux-form ’s Field component manages the actual inputs
including the validation that the eld requires.
// Components
import ReduxFormInput from '../../../components/Inputs/ReduxFormInput'
import Button from '../../../components/Button'
//Utils
import {
required,
email,
} from '../../../utils/formValidators'
import * as API from '../../utils/api'
class SignInForm extends Component {
handleSubmit = (user) => {
/*
* `user` is the object of values passed in from Redux Form
* user = {
* email: user.email,
* password: user.password
* }
*/
API.login(user).then(response => {
if (response.success)) {
// Do some stuff! The form worked :)
} else {
// Set an error message
}
})
}
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 4/10
7/14/2018 React + Redux Form | Alex Martinez
render() {
const { handleSubmit } = this.props
return (
<form onSubmit={handleSubmit(this.handleSubmit)}>
<Field
component={ReduxFormInput}
label='Email'
name='email'
placeholder='Enter your email'
type='email'
validate={[required, email]}
/>
<Field
component={ReduxFormInput}
label='Password'
name='password'
placeholder='Enter a password'
type='password'
validate={required}
/>
<Button
className='cta full-width invert'
>
Submit
</Button>
</form>
)
}
}
// Connect to redux-form
SignInForm = reduxForm({
form: 'SignInForm'
})(SignInForm)
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 5/10
7/14/2018 React + Redux Form | Alex Martinez
In the above example, you’ll see that Field takes a prop called component . This prop allows
you de ne your own input component and can be either a Component , a stateless function,
or a string name of one of the default supported DOM inputs ( input , textarea or
select ).
return (
<div
className={buildClassName([
'input-container',
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 6/10
7/14/2018 React + Redux Form | Alex Martinez
optionalClasses,
touched && error ? ' error' : '',
])}
>
{ label &&
<label>{label}</label>
}
<div className='input-container-inner'>
<input
{...input}
type={type}
placeholder={placeholder || label}
disabled={disabled || false}
/>
</div>
{touched && error &&
<div className="input-container-error-message">
{error}
</div>
}
</div>
)
}
}
Field-Level Validation
One of my favorite parts of Redux Form is how easy it makes validation. While Redux Form
provides several types of validation (Sync, Submit, Async Blur, and more), I mainly make use
of Field-Level Validation.
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 7/10
7/14/2018 React + Redux Form | Alex Martinez
Field-Level Validation focuses on Field ’s validate prop. It accepts one or more functions
to validate the value of the given eld. If an input doesn’t pass the requirements passed
through validate , the entire form won’t submit and an error prop is then passed to the
input allowing you to render a message to the user.
// Components
import ReduxFormInput from '../../../components/Inputs/ReduxFormInput'
// Utils
import { required, email } from '../../../utils/formValidators'
/* ... */
<Field
component={ReduxFormInput}
label='Email'
name='email'
placeholder='Enter your email'
type='email'
validate={[required, email]}
/>
/* ... */
Redux Form’s documentation provides example validation functions, but you can also create
custom functions to check input eld values any way you want.
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 8/10
7/14/2018 React + Redux Form | Alex Martinez
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 9/10
7/14/2018 React + Redux Form | Alex Martinez
Conclusion
React Form isn’t the only package out there to manage forms in React (see Formik, React
Redux Form, and React-Form to name a few), but it’s the one that’s been making my life a
whole lot easier.
Other Resources
cdbf
© ALEX MARTINEZ 2018. MADE WITH ♥ IN THE MIDWEST
https://alxmrtnz.com/thoughts/2018/05/02/react-and-redux-form.html 10/10