Field

The Field component is how you connect each individual input to the Redux store. There are three fundamental things that you need to understand in order to use Field correctly:

  1. The name prop is required. It is a string path, in dot-and-bracket notation, corresponding to a value in the form values. It may be as simple as 'firstName' or as complicated as contact.billing.address[2].phones[1].areaCode.

  2. The component prop is required. It may be a Component, a stateless function, or string name of one of the default supported DOM inputs (input, textarea or select), See Usage section below. To learn about the props that will provided to whatever your component is, see the Props section below.

  3. All other props will be passed along to the element generated by the component prop.

Importing


var Field = require('redux-form').Field;  // ES5

import { Field } from 'redux-form';  // ES6

Props you can pass to Field

name : String [required]

A string path, in dot-and-bracket notation, corresponding to a value in the form values. It may be as simple as 'firstName' or as complicated as contact.billing.address[2].phones[1].areaCode. See the Usage section below for details.

component : Component|Function|String [required]

A Component, stateless function, or string corresponding to a default JSX element. See the Usage section below for details.

defaultValue : any [optional]

A value to be used if the current value for this field is undefined in the Redux Store. Defaults to '' to ensure that the input is controlled.

withRef : boolean [optional]

If true, the rendered component will be available with the getRenderedComponent() method. Defaults to false. Cannot be used if your component is a stateless function component.

Usage

The component prop will be passed to React.createElement(), which accepts one of three possible things:

1. A component

This can be any component class that you have written or have imported from a third party library.


// MyCustomInput.js
import React, { Component } from 'react'

class MyCustomInput extends Component {
  render() {
    const { value, onChange } = this.props
    return (
      <div>
        <span>The current value is {value}.</span>
        <button type="button" onClick={() => onChange(value + 1)}>Inc</button>
        <button type="button" onClick={() => onChange(value - 1)}>Dec</button>
      </div>
    )
  }
}

Then, somewhere in your form...


import MyCustomInput from './MyCustomInput'

...

<Field component={MyCustomInput}/>

To learn what props will be passed to your component, see the Props section below.

2. A stateless function

This is the most flexible way to use <Field>, as it gives you complete control over how the input is rendered. It is especially useful for displaying validation errors. It will also be the most familiar to people migrating from previous versions of redux-form. You must define the stateless function outside of your render() method, or else it will be recreated on every render and will force the Field to rerender because its component prop will be different. If you are defining your stateless function inside of render(), it will not only be slower, but your input will lose focus whenever the entire form component rerenders.


// outside your render() method
const renderField = (props) => (
    <div class="input-row">
      <input type="text" {...props}/>
      {props.touched && props.error && <span className="error">{props.error}</span>}
    </div>
  )

// inside your render() method
<Field component={renderField}/>

To learn what props will be passed to your stateless function, see the Props section below.

3. A string: input, select, or textarea

So all you really need to render an redux-form-connected text input is:


<Field component="input" type="text"/>
Remember the second rule above that all other props are passed to the element generated by component? So the type="text" prop will be given to the input.

Instance API

The following properties and methods are available on an instance of a Field component.

dirty : boolean

true if the current value is different from the initialized value, false otherwise.

name : String

The name prop that you passed in.

pristine : boolean

true if the current value is the same as the initialized value, false otherwise.

value : any

The current value of the field.

getRenderedComponent()

Returns the instance of the rendered component. For this to work, you must provide a withRef prop, and your component must not be a stateless function component.

####

Props

These are props that Field will pass to your wrapped component.

active : boolean

true if this field currently has focus. It will only work if you are passing onFocus to your input element.

asyncValidating : boolean

true if the form is currently running asynchronous validation because this field was blurred.

checked : boolean [optional]

An alias for value only when value is a boolean. Provided for convenience of destructuring the whole field object into the props of a form element.

dirty : boolean

true if the field value has changed from its initialized value. Opposite of pristine.

error : String [optional]

The error for this field if its value is not passing validation. Both synchronous, asynchronous, and submit validation errors will be reported here.

invalid : boolean

true if the field value fails validation (has a validation error). Opposite of valid.

name : String

The name prop passed in.

onBlur(eventOrValue) : Function

A function to call when the form field loses focus. It expects to either receive the React SyntheticEvent or the current value of the field.

onChange(eventOrValue) : Function

A function to call when the form field is changed. It expects to either receive the React SyntheticEvent or the new value of the field.

onDragStart(event) : Function

A function to call when the form field receives a dragStart event. Saves the field value in the event for giving the field it is dropped into.

onDrop(event) : Function

A function to call when the form field receives a drop event.

onFocus(event) : Function

A function to call when the form field receives focus.

onUpdate(eventOrValue) : Function

An alias for onChange. Provided for convenience of destructuring the whole field object into the props of a form element. Added to provide out-of-the-box support for Belle components' onUpdate API.

pristine : boolean

true if the field value is the same as its initialized value. Opposite of dirty.

touched : boolean

true if the field has been touched. By default this will be set when the field is blurred.

valid : boolean

true if the field value passes validation (has no validation errors). Opposite of invalid.

value: any

The value of this form field. It will be a boolean for checkboxes, and a string for all other input types. If there is no value in the Redux state for this field, it will default to the defaultValue prop given to Field. If no such defaultValue is specified, it will be ''. This is to ensure that the input is controlled.

visited: boolean

true if this field has ever had focus. It will only work if you are passing onFocus to your input element.