useListFormContext
@spartanfx/react / spfx / useListFormContext
Function: useListFormContext()
function useListFormContext(config?): IListFormContext;
Custom React hook to initialize or consume a List Form context within an SPFx environment.
When called with a configuration object, it creates a new context instance and manages:
- SharePoint list metadata
- List item loading (new or existing)
- Field schema and state mapping
- Validation state and error messages
- Attachment management
- Data binding via
formStatehelpers - Saving list item back to SharePoint
When called with no parameters, it consumes the context from a parent <ListFormContext.Provider>.
Configuration
Required
| Property | Type | Description |
|---|---|---|
listName | string | The display name of the SharePoint list. |
sp | SPFI | PnPjs SPFI instance used for SharePoint REST API operations. |
webPartContext | WebPartContext | SPFx WebPart context, typically passed from the WebPart class. |
Options
Events & callbacks
Full type reference:
IListFormConfig
Returns
An instance of IListFormContext providing access to form state, validation, and persistence APIs.
Throws
Error if no config is passed and no parent context is available.
Examples
Initializing the listFormContext
const ctx = useListFormContext({
sp,
listName: 'Projects',
itemId: 5,
webPartContext,
onInit: async (ctx) => {
ctx.formState.setText('Title', 'Initial Value');
}
});
Consuming the list formContext
useListFormContext()