React Advanced Form
  • Introduction
  • General
    • Comparison
      • Compared to Formik
      • Compared to Final form
    • Migration guides
      • 1.4.x → 1.5.x
    • FAQ
  • Getting started
    • Installation
    • Creating fields
    • Creating form
    • Validation rules
    • Validation messages
    • Applying validation
    • Handle submit
  • Architecture
    • Argument properties
    • Referencing
    • Field lifecycle
    • Controlled fields
    • Reactive props
  • Validation
    • Getting started
    • Validation schema
      • Rule definition
      • Reactive rule
    • Validation messages
  • High-order components
    • createField
      • Options
      • Field presets
      • Exposed props
  • Components
    • FormProvider
    • Form
      • Props
        • innerRef
        • initialValues
        • action
        • rules
        • messages
      • Methods
        • setValues()
        • setErrors()
        • reset()
        • validate()
        • serialize()
        • submit()
      • Callbacks
        • onFirstChange
        • onReset
        • onInvalid
        • onSerialize
        • onSubmitStart
        • onSubmitted
        • onSubmitFailed
        • onSubmitEnd
    • Field.Group
    • Field
      • Props
        • rule
        • asyncRule
        • skip
      • Callbacks
        • onFocus
        • onChange
        • onBlur
  • Recipes
    • Generating a form
    • Utilizing functions
  • Developers
    • Contributing
Powered by GitBook
On this page
  • Specification
  • Definition
  • Parameters
  • Default behavior
  • Example
  1. Components
  2. Field
  3. Callbacks

onChange

Event handler called when a field loses focus.

Specification

Event handler called on each field value change.

Definition

type OnChange = (params) => void

Parameters

Parameter name

Type

Description

event

Event

Native event reference.

prevValue

any

The previous value of a field.

nextValue

any

The next value of a field.

fieldProps

Object

Props of the current field.

fields

Object

Reference to all fields of a form.

form

Object

Form component reference.

Default behavior

By default, onChange behaves as a callback method, allowing you to base your external logic based on the field updates. This doesn't change the controlled flow of the field.

The same onChange method can be used as the handler when working with controlled fields. In that case it is responsible for updating the data source used for setting a value on the field.

Example

import React from 'react'
import { Form } from 'react-advanced-form'
import { Input } from 'react-advanced-form-addons'

export default class Example extends React.Component {
  handleUsernameChange = ({
    event,
    nextValue,
    prevValue,
    fieldProps,
    fields,
    form
  }) => {
    // ...
  }

  render() {
    return (
      <Form>
        <Input
          name="username"
          onChange={this.handleUsernameChange}
          required />
      </Form>
    )
  }
}
PreviousonFocusNextonBlur

Last updated 6 years ago

Controlled fields