SharePoint Framework - Handle Web Part Property Changes

Overview

 
SharePoint web parts, since the Classic SharePoint era, have supported properties. They are generally referred to as web part properties which allow configuring the web part behavior based on the value specified. We are gradually moving towards Modern SharePoint. The SharePoint Framework (SPFx) based web parts do also support web part properties. As the modern SharePoint is based on HTML and JavaScript, it offers more flexibility to control the behavior of the SharePoint Framework client web parts. Please refer to my previous article to get started with property panes in SharePoint Framework.
 
In this article, we will explore how we can handle the property changes in SharePoint Framework client web parts.
 

Develop SharePoint Framework Web Part

 
Open the command prompt. Create a directory for SPFx solution.
  1. md spfx-handle-property-changes  
Navigate to the above-created directory.
  1. cd spfx-handle-property-changes  
Run Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
 
SharePoint Framework - Handle Web Part Property Changes 
 
Solution Name: Hit Enter to have a default name (spfx-handle-property-changes in this case) or type in any other name for your solution.
Selected choice: Hit Enter
 
Target for the component: Here, we can select the target environment where we are planning to deploy the client webpart, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
 
Place of files: We may choose to use the same folder or create a subfolder for our solution.
Selected choice: Same folder
 
Deployment option: Selecting Y will allow the app to be deployed instantly to all the sites and to be accessible everywhere.
Selected choice: N (install on each site explicitly)
 
Type of client-side component to create: We can choose to create client-side webpart or an extension. Choose a webpart option.
Selected choice: WebPart
 
Web part name: Hit enter to select the default name or type in any other name.
Selected choice: HandlePropertyPaneChanges
 
Web part description: Hit enter to select the default description or type in any other value.
Selected choice: Handle property pane changes in SPFx
 
Framework to use: Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)
Selected choice: No JavaScript Framework
 
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command.
  1. npm shrinkwrap  
In the Command Prompt, type the below command to open the solution in a code editor of your choice.
  1. code .  

Run the SPFx WebPart

  1. On the command prompt, type “gulp serve”.
  2. Open SharePoint site.
  3. Navigate to /_layouts/15/workbench.aspx.
  4. Add the webpart to the page.

    SharePoint Framework - Handle Web Part Property Changes

  5. Edit the web part and update the description property

    SharePoint Framework - Handle Web Part Property Changes 
You will notice that, as we type in the values for the description, it is getting reflected right away in the web part. For a Hello World kind of webpart, it is fine. However, we do not always want to fire events on each key down and update the webpart. This situation can be handled by some tweak in webpart.
 

Handle the web part property changes

 
Property pane has two interaction modes,
  • Reactive: When property field is modified, a change event is triggered and it updates the webpart with new values
  • Non-reactive: It updates web part with new values only after user confirmation by clicking Apply button from property pane.

Code the WebPart

Open our web part class - HandlePropertyChangesWebPart.ts under “\src\webparts\handlePropertyChanges\” folder.
 
Add the below method,
  1. protected get disableReactivePropertyChanges(): boolean {   
  2.   return true;   
  3. }  
While the “gulp server” is running refresh the page and edit the webpart. The changes to property fields are now non-reactive and the user has to click the Apply button to see the changes reflected in the web part.
 
SharePoint Framework - Handle Web Part Property Changes 
 

Summary

 
While the reactive mode is spontaneous to reflect the web part property changes, in some cases, we need to have a non-reactive mode to allow the user to select the values for property fields and confirm the changes. Having disableReactivePropertyChanges implemented in SPFx web part will allow controlling this behavior.