isNewItemCheck
A function that determines whether the form should operate in create mode or edit mode. When the form component mounts, <ReactForm> calls isNewItemCheck with the context. Within context, you can find itemId prop and it's value which can be useful to identify the mode.
jsx
{
isNewItemCheck: (context) => {
const { itemId } = context;
// Return true for create mode, false for update mode
return itemId === "create";
};
}- Returns
true: Form enters create mode. No API call is made; default field values are used. - Returns
false: Form enters edit mode. Thereadfunction is called withitemIdto fetch existing data.
Most applications use the same component for both creating and editing records. The URL pattern typically looks like:
https://localhost:7777/users/create // Create mode
https://localhost:7777/users/7 // Edit mode (ID: 7)Based on the params received from URL here, "create" means a new user needs to be created. A unique ID means we need to open this form in edit mode and hence call read function with the "7" as argument.
Usage
Via ReactFormProvider (Global)
jsx
<ReactFormProvider
isNewItemCheck={({ itemId }) => {
return itemId === "create";
}}
>
<App />
</ReactFormProvider>Via ReactForm (Form-specific)
jsx
<ReactForm
schema={schema}
itemId={itemId}
isNewItemCheck={({ itemId }) => {
return itemId === "new";
}}
create={create}
update={update}
>
{({ context }) => (
// Form content
)}
</ReactForm>