Reputation: 2230
I am newbie to redux .I'm trying to create an application using redux-form. Could you please help me how to trigger an action when any changes happen in any of the field in the form .
import React from 'react'
import { Field, reduxForm } from 'redux-form'
import { Col, Button, Form, FormGroup, Label, Input, FormText } from 'reactstrap';
const renderField=({ input, label,name, type, meta})=>(
<FormGroup row>
<Label for={name} sm={2}>{label}</Label>
<Col sm={10}>
<input {...input} placeholder={label} type={type}></input>
</Col>
</FormGroup>
)
const BreakupForm=(props)=>{
console.log(props);
const { handleSubmit, pristine, reset, submitting } = props
return(
<div>
<Label >Breakup</Label>
<form onSubmit={handleSubmit}>
<Field label="Basic" name="basic" component={renderField} type="text" placeholder="basic"/>
<Field label="HRA" name="hra" component={renderField} type="text" placeholder="HRA"/>
<Field label="Transport Allowance" name="ta" component={renderField} type="text" placeholder="Transport Allowance" />
<Field label="Special Allowance" name="specialAllowance" component={renderField} type="text" placeholder="Special Allowance" />
<Field label="LTA" name="lta" component={renderField} type="text" placeholder="LTA"/>
<Field label="Medical Bills" name="medicalBills" component={renderField} type="text" placeholder="Medical Bills"/>
</form>
</div>
)
}
export default reduxForm({
form: 'breakupForm' // a unique identifier for this form
})(BreakupForm)
Upvotes: 0
Views: 4340
Reputation: 9338
Well, according to Redux Form documentation, Redux Form onChange callback
Field component accepts a prop (onChange), you can pass a function to the onChange props.
<Field label="Basic" onChange={this.handleChange} name="basic" component={renderField} type="text" placeholder="basic"/>
handleChange will be a function defined in your class.
Since your component is stateless component, you can passs the callback function through props
<Field label="Basic" onChange={props.handleChange} name="basic" component={renderField} type="text" placeholder="basic"/>
Upvotes: 0
Reputation: 6280
You can use onChange
on every field you want to observe, for instance:
<Field label="Basic" name="basic" component={renderField} type="text" placeholder="basic" onChange={onChangeHandler} />
and then write your onChangeHandler
which will be called with the following parameters:
event, newValue, previousValue, name
For instance, looking at your code, you can listen to field basic
changes like this:
const BreakupForm = (props) => {
const onBasicFieldChange = (event, newValue, previousValue, name) => {
console.log(newValue)
}
console.log(props);
const { handleSubmit, pristine, reset, submitting } = props
return (
<div>
<Label >Breakup</Label>
<form onSubmit={handleSubmit}>
<Field onChange={onBasicFieldChange} label="Basic" name="basic" component={renderField} type="text" placeholder="basic" />
<Field label="HRA" name="hra" component={renderField} type="text" placeholder="HRA" />
<Field label="Transport Allowance" name="ta" component={renderField} type="text" placeholder="Transport Allowance" />
<Field label="Special Allowance" name="specialAllowance" component={renderField} type="text" placeholder="Special Allowance" />
<Field label="LTA" name="lta" component={renderField} type="text" placeholder="LTA" />
<Field label="Medical Bills" name="medicalBills" component={renderField} type="text" placeholder="Medical Bills" />
</form>
</div>
)
}
Upvotes: 1