UI KIT view.getContext() is not working

Hello,
I’m having trouble getting the context from view.getContext(), and I’m not sure how to resolve it. I currently have a UI Kit 1 application, which is deprecated, so I plan to switch to a newer version.

const ViewAttachmentLabels = () => {
    const [context, setContext] = useState(undefined);
    useEffect(() => {
        view.getContext().then((c) => setContext(c))
    }, [])
    console.log("CONTEXT:", context);
    const {
        extensionContext: { fieldType, fieldId, fieldValue },
        moduleKey,
        accountId,
        cloudId,
        platformContext: { issueId, issueTypeId, projectId },
    } = context;

    const hasLicense = isLicenseActive(context);
    if (!hasLicense) {
        return (
            <CustomField>
                <Text>{LICENSE_INVALID}</Text>
            </CustomField>
        )
    }

    if (!fieldValue) {
        return (<Badge>
            <Text>
                -
            </Text>
        </Badge>)
    }
    return (
        <Badge>
            <Text>
                {fieldType.includes(MULTI) ? fieldValue.join(", ") : fieldValue}
            </Text>
        </Badge>
    )
}

ForgeReconciler.render(
    <React.StrictMode>
        <ViewAttachmentLabels />
    </React.StrictMode>
);

I’m not sure exactly which error you’re trying to get around, but on a quick glance, I’d say there’s likely an issue that you’re destructuring context before it’s been set in useEffect. On initial load, it’s going to be undefined, so I’d guess you have an issue with destructuring it. You could set defaults for that, even as empty objects, if that’s the error.

2 Likes

Hi @MertcanKaraba , the shape of the context object is different in UI Kit. You can see properties here - https://developer.atlassian.com/platform/forge/manifest-reference/modules/jira-custom-field/#extension-data . extensionContext and platformContext no longer exist and their properties can be access directly from the context object itself

1 Like