{"id":29083,"date":"2021-08-20T11:02:40","date_gmt":"2021-08-20T11:02:40","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=29083"},"modified":"2021-10-07T11:30:57","modified_gmt":"2021-10-07T06:00:57","slug":"how-to-programmatically-call-updateview-in-pcf-using-requestrender","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2021\/08\/how-to-programmatically-call-updateview-in-pcf-using-requestrender\/","title":{"rendered":"How to programmatically call UpdateView in PCF using requestRender"},"content":{"rendered":"<h2 style=\"text-align: justify;\"><strong>Introduction:<\/strong><\/h2>\n<p style=\"text-align: justify;\">If you know Power Apps Component Framework, then you must know the <strong>updateView<\/strong> method, which is one of the standard methods of PCF.<\/p>\n<p style=\"text-align: justify;\">The <strong>updateView<\/strong> method is triggered only when the PCF control is refreshed or rendered and shows us the updated data on our control.<\/p>\n<p style=\"text-align: justify;\">We work on the dataset as well as field type components in PCF. In the dataset, we usually use <strong>context.dataset.refresh<\/strong> to refresh the dataset control, which calls the <strong>updateView<\/strong> method. Some users also use <strong>setState<\/strong> method to re-render a particular component. However, sometimes it becomes difficult to manage the data in the state.<\/p>\n<p style=\"text-align: justify;\">In addition, on using <strong>setState<\/strong> only the component for which the state is changed gets re-rendered while all the other components in the PCF control remains as it is.<\/p>\n<p style=\"text-align: justify;\">We recently had a requirement to refresh a field component. However, there is no appropriate refresh method for field component, which can work as same as <strong>context.dataset.refresh<\/strong> method.<\/p>\n<p style=\"text-align: justify;\">So to overcome this limitation, let me introduce you a method named as <strong>requestRender<\/strong>. It is available for both dataset as well as field components, using this method we can re-render the PCF control. You can use this method as shown here, <strong>context.factory.requestRender().<\/strong><\/p>\n<p style=\"text-align: justify;\">It is similar to refresh method in dataset control; <strong>requestRender<\/strong> calls the <strong>updateView<\/strong> method and re-renders the control.<\/p>\n<h2 style=\"text-align: justify;\"><strong>Scenario:<\/strong><\/h2>\n<p style=\"text-align: justify;\">Let\u2019s say we are using a single fluent UI component i.e., contextual menu and on click of its menu items, we want to set the button text to the selected menu item as shown below.<\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/image001-ink-18.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-29090 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/image001-ink-18.png\" alt=\"How to programmatically call UpdateView in PCF using requestRender\" width=\"322\" height=\"248\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/image001-ink-18.png 322w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/image001-ink-18-300x231.png 300w\" sizes=\"(max-width: 322px) 100vw, 322px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/image003-ink-16.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-29091 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/image003-ink-16.png\" alt=\"How to programmatically call UpdateView in PCF using requestRender\" width=\"240\" height=\"69\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">To achieve this, we can use setState, but sometimes it becomes difficult to maintain the state. Here, to make our work simple yet efficient, <strong>requestRender<\/strong> comes to the rescue.<\/p>\n<p style=\"text-align: justify;\">Below is our Contextual Menu component class code, which we are calling from <strong><em>index.ts<\/em><\/strong> file by passing context as props in it.<\/p>\n<p style=\"text-align: justify;\">Here, we selected Tomorrow option and it gets set as the button label.<\/p>\n<h2 style=\"text-align: justify;\"><strong>Code:<\/strong><\/h2>\n<pre class=\"lang:css gutter:true start:1\">export class FluentUIContextualMenu extends React.Component&lt;any, any&gt; {\r\nprivate _buttonText: string = \"Later Today\";\r\nconstructor(props: any) {\r\nsuper(props);\r\n}\r\nrender(): JSX.Element {\r\nlet menuItems: IContextualMenuItem[] = this.getMenuItems();\r\nreturn (\r\n&lt;&gt;\r\n&lt;DefaultButton text={this._buttonText}\r\nmenuProps={\r\n{\r\nshouldFocusOnMount: true,\r\ndirectionalHint: DirectionalHint.bottomLeftEdge,\r\nitems: menuItems\r\n}\r\n}\r\n\/&gt;\r\n&lt;\/&gt;\r\n)\r\n}\r\nprivate getMenuItems = (): IContextualMenuItem[] =&gt; {\r\nlet menuItems: any = [];\r\nmenuItems = [\r\n{\r\nkey: 'Later Today',\r\niconProps: { iconName: 'Clock' },\r\ntext: 'Later Today',\r\nonClick: (event: React.MouseEvent&lt;HTMLAnchorElement | HTMLButtonElement&gt;, item: IContextualMenuItem) =&gt; {\r\nthis.onMenuItemClick(event, item);\r\n}\r\n},\r\n{\r\nkey: 'Tomorrow',\r\niconProps: { iconName: 'Coffeescript' },\r\ntext: 'Tomorrow',\r\nonClick: (event: React.MouseEvent&lt;HTMLAnchorElement | HTMLButtonElement&gt;, item: IContextualMenuItem) =&gt; {\r\nthis.onMenuItemClick(event, item);\r\n}\r\n},\r\n{\r\nkey: 'This Weekend',\r\niconProps: { iconName: 'Vacation' },\r\ntext: 'This Weekend',\r\nonClick: (event: React.MouseEvent&lt;HTMLAnchorElement | HTMLButtonElement&gt;, item: IContextualMenuItem) =&gt; {\r\nthis.onMenuItemClick(event, item);\r\n}\r\n},\r\n{\r\nkey: 'Next Week',\r\niconProps: { iconName: 'Suitcase' },\r\ntext: 'Next Week',\r\nonClick: (event: React.MouseEvent&lt;HTMLAnchorElement | HTMLButtonElement&gt;, item: IContextualMenuItem) =&gt; {\r\nthis.onMenuItemClick(event, item);\r\n}\r\n},\r\n];\r\nreturn menuItems;\r\n}\r\nprivate onMenuItemClick = (event: React.MouseEvent&lt;HTMLAnchorElement | HTMLButtonElement&gt;, item: IContextualMenuItem) =&gt; {\r\nthis._buttonText = item.text != undefined ? item.text : this._buttonText;\r\nthis.props.context.factory.requestRender();\r\n}\r\n}\r\n<\/pre>\n<h2 style=\"text-align: justify;\"><strong>Explanation:<\/strong><\/h2>\n<p style=\"text-align: justify;\">In the function <strong>onMenuItemClick<\/strong>, the global variable <strong>this._buttonText<\/strong> takes the updated text from contextual menu item and <strong>this.props.context.factory.requestRender(); <\/strong>renders the control, which calls the <strong>updateView<\/strong> method and the selected text remains intact in the global variable that shows the updated text on the contextual menu button.<\/p>\n<p style=\"text-align: justify;\">Similarly, as we know, there is no refresh method available for the field component, unlike the dataset, which has one. We can use the <strong>requestRender<\/strong> method in the field component to refresh it.<\/p>\n<p style=\"text-align: justify;\">Another good thing about the <strong>requestRender<\/strong> method is that, similar to the setState it maintains and gives the updated data to the user.<\/p>\n<p style=\"text-align: justify;\"><strong>Conclusion: <\/strong>In this blog, we learned how to call the <strong>UpdateView<\/strong> method programmatically using <strong>RequestRender<\/strong>, which eventually helps in refreshing the entire PCF control thus helping us see our changes efficiently.<\/p>\n<p style=\"text-align: justify;\">Reference: <a href=\"https:\/\/docs.microsoft.com\/en-us\/powerapps\/developer\/component-framework\/reference\/factory\/requestrender\">https:\/\/docs.microsoft.com\/en-us\/powerapps\/developer\/component-framework\/reference\/factory\/requestrender<\/a><\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/map-my-relationships-dynamics-365-crm\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"wp-image-28887 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/mmr.jpg\" alt=\"Map My Relationships\" width=\"800\" height=\"200\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/mmr.jpg 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/mmr-300x75.jpg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/mmr-768x192.jpg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/08\/mmr-660x165.jpg 660w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: If you know Power Apps Component Framework, then you must know the updateView method, which is one of the standard methods of PCF. The updateView method is triggered only when the PCF control is refreshed or rendered and shows us the updated data on our control. We work on the dataset as well as\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2021\/08\/how-to-programmatically-call-updateview-in-pcf-using-requestrender\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[44,1929,2361],"tags":[1941,1881],"class_list":["post-29083","post","type-post","status-publish","format-standard","hentry","category-power-apps","category-pcf","category-technical","tag-pcf-control","tag-powerapps-dynamics-365"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/29083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=29083"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/29083\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=29083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=29083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=29083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}