Connected forms
Connected Forms are bound to a model in your app's data schema. Whenever a connected form is submitted, a record is automatically created or updated in the bound data model, with some or all of the form's input fields mapping to fields in the data model. Connected forms automatically work with any Amplify GraphQL API, and no onSubmit
handling is required.
Generate forms
First, install the Amplify UI library.
npm add @aws-amplify/ui-react
To use connected forms, you first need to deploy a data model from your sandbox environment. We will use the same example as in the getting started tutorial. To get started run the following command from your project root:
npx ampx generate forms
This will generate create and update forms for each model defined in your schema in a folder called ui-components
.
File written: ui-components/graphql/subscriptions.tsFile written: ui-components/graphql/mutations.tsFile written: ui-components/graphql/queries.tsFile written: ui-components/TodoCreateForm.jsxFile written: ui-components/TodoCreateForm.d.tsFile written: ui-components/TodoUpdateForm.jsxFile written: ui-components/TodoUpdateForm.d.tsFile written: ui-components/utils.jsFile written: ui-components/index.js
Re-generating forms
In Gen 2, we automatically generate the form UI for you, which you can then customize and manage. If you decide to update your data model and need to regenerate the forms, please ensure you back up the original ui-components
folder before executing the npx ampx generate forms
command again.
Render React form in your app
- In your application's entrypoint file (e.g.
src/index.js
for create-react-app orsrc/main.jsx
for Vite), add the following imports and configuration
import '@aws-amplify/ui-react/styles.css';import { ThemeProvider } from '@aws-amplify/ui-react';import { Amplify } from 'aws-amplify';
import outputs from './amplify_outputs.json';
Amplify.configure(outputs);
- In your application's entrypoint file (e.g.
src/main.jsx
for Vite), wrap the<App />
component with the following:
<ThemeProvider> <App /></ThemeProvider>
- Import your form by name. For a form named
TodoCreateForm
, you would use the following code:
import { TodoCreateForm } from './ui-components';
- Place your form in code. For a form named
ProductCreateForm
in a React project, you could use the following App code:
function App() { return <TodoCreateForm />;}
export default App;
Types of forms
All connected and unconnected forms are either a Create form or an Update form.
Create forms
Create forms render a form with empty inputs. If a create form is connected to a data model, will always generate a new record upon submission.
Update forms
Update forms expect an input value in order to pre-populate the form.
For update forms that are connected to a data model, you can use the id
prop, or the model prop:
id
prop: id string of the record you want to update. For example:
<AuthorUpdateForm id="ac74af5c-3aab-4274-8f41-23e1e6576af5" />
- Model prop: if your form is bound to a data model named
Author
, your form will have a prop namedauthor
as well, which can receive a record. For example:
<AuthorUpdateForm author={authorRecord}>