Skip to main content

getContext

@spartanfx/react v1.2.2


@spartanfx/react / spfx / getContext

Function: getContext()

function getContext(context?): WebPartContext;

Retrieves or initializes the current WebPartContext.

Parameters​

context?​

WebPartContext

Optional SPFx web part context to initialize or update the stored reference.

Returns​

WebPartContext

The currently stored SPFx WebPartContext.

Remarks​

This function ensures that a valid SharePoint Framework (SPFx) context is available throughout the application. If a context is provided, it updates the stored reference; otherwise, it returns the previously stored one.

This method should be called early in the lifecycle of a web part or extension to make the SPFx context available to other helpers such as getSP and getGraph.

Example​

// Initialize context during web part render
const ctx = getContext(this.context);

// Retrieve stored context later
const storedCtx = getContext();