Tracking component add and remove events
Overview
FormEngine Designer provides two powerful callback functions that allow you to track when components are added to or removed from your form: onFormElementAdd and onFormElementRemove. These callbacks are essential for implementing logic that needs to respond to changes in your form structure.
Common use cases
- Analytics: Track which components are most frequently added to forms
- Logging: Maintain audit trails of form design changes
- Validation: Enforce business rules about component combinations
- Limits: Restrict the number of specific component types
- Integration: Sync form changes with external systems
The onFormElementAdd callback
The onFormElementAdd callback is invoked whenever a component is added to the form,
whether through drag-and-drop, the components modal, or any other method.
Callback signature
function onFormElementAdd(payload: {
source: ComponentData;
target: ComponentData;
insertPosition?: InsertPosition;
slot?: string;
slotCondition?: string;
})
Parameters
- source: The component data being added to the form
- target: The parent component where the new component is being added
- insertPosition: Where the component is being inserted relative to the target (
'before','after', or'inside') - slot: The slot name if the component is being added to a specific slot
- slotCondition: The condition associated with the slot
Example usage
import {FormBuilder} from '@react-form-builder/designer'
const FormBuilderApp = () => {
return (
<FormBuilder
// ... other props
onFormElementAdd={(payload) => {
console.log('Component added:', {
componentType: payload.source.model.type,
componentKey: payload.source.key,
parentType: payload.target.model.type,
parentKey: payload.target.key,
position: payload.insertPosition,
timestamp: new Date().toISOString()
})
// Send to analytics service
analytics.track('component_added', {
componentType: payload.source.model.type,
insertionMethod: 'designer'
})
// Enforce business rules
if (payload.source.model.type === 'RsInput') {
const inputCount = countComponentsByType('RsInput')
if (inputCount > 10) {
console.warn('Maximum number of input fields reached')
}
}
}}
/>
)
}
The onFormElementRemove callback
The onFormElementRemove callback is invoked whenever a component is removed from the form, typically when the user deletes a component.
Callback signature
function onFormElementRemove(payload: { source: ComponentData })
Parameters
- source: The component data being removed from the form
Example usage
import {FormBuilder} from '@react-form-builder/designer'
const FormBuilderApp = () => {
return (
<FormBuilder
// ... other props
onFormElementRemove={(payload) => {
console.log('Component removed:', {
componentType: payload.source.model.type,
componentKey: payload.source.key,
timestamp: new Date().toISOString()
})
// Send to analytics service
analytics.track('component_removed', {
componentType: payload.source.model.type
})
// Clean up external resources
cleanupComponentResources(payload.source.key)
}}
/>
)
}
Best practices
- Keep callbacks lightweight: Avoid heavy computations in callbacks as they execute during user interactions.
- Handle errors gracefully: Wrap your callback logic in try-catch blocks to prevent UI disruptions.
- Use useCallback for performance: Wrap callback functions in
useCallbackwith appropriate dependencies to prevent unnecessary re-renders. - Consider debouncing: For analytics or external API calls, consider debouncing to avoid excessive requests.
- Respect user privacy: When tracking analytics, ensure compliance with privacy regulations and provide opt-out mechanisms.
Common issues and solutions
Callback not firing
- Ensure you're passing the callback correctly to the
FormBuildercomponent - Check that the component is actually being added/removed (not just moved)
- Verify there are no errors in your callback that might be silently failing
Performance concerns
- If callbacks are causing performance issues, consider throttling or debouncing
- Move heavy computations to Web Workers or defer them
- Use
useCallbackto prevent unnecessary re-renders
Related resources
Summary
The onFormElementAdd and onFormElementRemove callbacks provide powerful hooks into the form design process,
enabling you to build sophisticated features on top of FormEngine Designer.
Whether you need analytics, validation, integration, or custom business logic,
these callbacks give you the visibility and control you need.
Key takeaways:
- Use
onFormElementAddto track component additions with detailed context - Use
onFormElementRemoveto track component removals - Both callbacks receive
ComponentDatawith full component information - Implement error handling to prevent UI disruptions
- Consider performance implications for complex operations
With these callbacks, you can transform FormEngine Designer from a simple form builder into a fully integrated part of your application ecosystem.