Skip to main content
Version: Next

Hooks

formio-react-native provides several custom React hooks to simplify common tasks and enhance form interactivity.

useFormioForm

This hook provides access to form instance and methods.

Parameters

ParameterTypeDescription
formobjectThe form schema or FormioForm instance.
optionsobjectOptions to configure the form.

Returns

Return ValueTypeDescription
formInstanceobjectThe FormioForm instance.
submissionobjectThe current form submission data.
errorsobjectAny validation errors.
setSubmissionfunctionFunction to update the form's submission.
submitFormfunctionFunction to programmatically submit the form.

Usage Example

import React from 'react';
import { View, Button, Alert } from 'react-native';
import { FormioForm, useFormioForm } from 'formio-react-native';

const myFormSchema = {
components: [
{
type: 'textfield',
key: 'name',
label: 'Name',
validate: { required: true },
},
{
type: 'button',
action: 'submit',
label: 'Submit (Internal)',
},
],
};

function MyFormWithHook() {
const { formInstance, submission, setSubmission, submitForm } = useFormioForm(
myFormSchema,
);

const handleExternalSubmit = () => {
// Programmatically submit the form
submitForm();
};

const handleFormSubmit = (data: any) => {
Alert.alert('Form Submitted', JSON.stringify(data, null, 2));
};

return (
<View style={{ flex: 1, padding: 20 }}>
<FormioForm
form={formInstance} // Pass the formInstance from the hook
submission={submission}
onSubmit={handleFormSubmit}
/>
<Button title="Submit Externally" onPress={handleExternalSubmit} />
</View>
);
}

export default MyFormWithHook;