SPFx Extension - Page Context is missing
-
31-12-2020 - |
Question
Recently I noticed, SPFx Extension is updated from drop build to RC (release candidate) build, because of that one of my SPFx Extension Application Customizer
stopped working. I went to SPFx Extension documents and most of the things are updated in document and following them I was able to make my application customizer working.
But earlier, there was page context (this.context.pageContext
object) variable available which is now removed from RC. I was using page context to get Web absolute url to do some REST API data pull and using site logo
property to get the logo.
Is there any alternative available to get the page context in RC or it is completely removed?
Solution
No its not removed or deprecated. Its working correctly . Just upgraded a sample and was able to work with the this.context
object without any issue.
You need to ensure that you are using it inside the onInit()
method because this event occurs after this.context
is populated.
If you are using it before the onInit
method, it will be undefined.
I have used the below sample code and it correctly logs the value in console (see following screenshot):
export default class TestappApplicationCustomizer
extends BaseApplicationCustomizer<ITestappApplicationCustomizerProperties> {
@override
public onInit(): Promise<void> {
console.log(this.context.pageContext.web.absoluteUrl);
return Promise.resolve<void>();
}
}