{"kind":"AgentDefinition","metadata":{"namespace":"community","name":"pcf-best-practices","version":"0.1.0"},"spec":{"agents_md":"---\ndescription: 'Best practices and guidance for developing PCF code components'\napplyTo: '**/*.{ts,tsx,js,json,xml,pcfproj,csproj,css,html}'\n---\n\n# Best Practices and Guidance for Code Components\n\nDeveloping, deploying, and maintaining code components needs a combination of knowledge across multiple areas. This article outlines established best practices and guidance for professionals developing code components.\n\n## Power Apps Component Framework\n\n### Avoid Deploying Development Builds to Dataverse\n\nCode components can be built in [production or development mode](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/code-components-alm#building-pcfproj-code-component-projects). Avoid deploying development builds to Dataverse since they adversely affect the performance and can even get blocked from deployment due to their size. Even if you plan to deploy a release build later, it can be easy to forget to redeploy if you don't have an automated release pipeline. More information: [Debugging custom controls](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/debugging-custom-controls).\n\n### Avoid Using Unsupported Framework Methods\n\nThese include using undocumented internal methods that exist on the `ComponentFramework.Context`. These methods might work but, because they're not supported, they might stop working in future versions. Use of control script that accesses host application HTML Document Object Model (DOM) isn't supported. Any parts of the host application DOM that are outside the code component boundary, are subject to change without notice.\n\n### Use `init` Method to Request Network Required Resources\n\nWhen the hosting context loads a code component, the `init` method is first called. Use this method to request any network resources such as metadata instead of waiting for the `updateView` method. If the `updateView` method is called before the requests return, your code component must handle this state and provide a visual loading indicator.\n\n### Clean Up Resources Inside the `destroy` Method\n\nThe hosting context calls the `destroy` method when a code component is removed from the browser DOM. Use the `destroy` method to close any `WebSockets` and remove event handlers that are added outside of the container element. If you're using React, use `ReactDOM.unmountComponentAtNode` inside the `destroy` method. Cleaning up resources in this way prevents any performance issues caused by code components being loaded and unloaded within a given browser session.\n\n### Avoid Unnecessary Calls to Refresh on a Dataset Property\n\nIf your code component is of type dataset, the bound dataset properties expose a `refresh` method that causes the hosting context to reload the data. Calling this method unnecessarily impacts the performance of your code component.\n\n### Minimize Calls to `notifyOutputChanged`\n\nIn some circumstances, it's undesirable for updates to a UI control (such as keypresses or mouse move events) to each call `notifyOutputChanged`, as more calls would result in many more events propagating to the parent context than needed. Instead, consider using an event when a control loses focus, or when the user's touch or mouse event completes.\n\n### Check API Availability\n\nWhen developing code components for different hosts (model-driven apps, canvas apps, portals), always check the availability of the APIs you're using for support on those platforms. For example, `context.webAPI` isn't available in canvas apps. For individual API availability, see [Power Apps component framework API reference](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/reference/).\n\n### Manage Temporarily Null Property Values Passed to `updateView`\n\nNull values are passed to the `updateView` method when data isn't ready. Your components should account for this situation and expect that the data could be null, and that a subsequent `updateView` cycle can include updated values. `updateView` is available for both [standard](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/reference/control/updateview) and [React](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/reference/react-control/updateview) components.\n\n## Model-Driven Apps\n\n### Don't Interact Directly with `formContext`\n\nIf you have experience working with client API, you might be used to interacting with `formContext` to access attributes, controls, and call API methods such as `save`, `refresh`, and `setNotification`. Code components are expected to work across various products like model-driven apps, canvas apps, and dashboards, therefore they can't have a dependency on `formContext`.\n\nA workaround is to make the code component bound to a column and add an `OnChange` event handler to that column. The code component can update the column value, and the `OnChange` event handler can access the `formContext`. Support for the custom events will be added in the future, which will enable communicating changes outside of a control without adding a column configuration.\n\n### Limit Size and Frequency of Calls to the `WebApi`\n\nWhen using the `context.WebApi` methods, limit both the number of calls and the amount of data. Each time you call the `WebApi`, it counts towards the user's API entitlement and service protection limits. When performing CRUD operations on records, consider the size of the payload. In general, the larger the request payload, the slower your code component is.\n\n## Canvas Apps\n\n### Minimize the Number of Components on a Screen\n\nEach time you add a component to your canvas app, it takes a finite amount of time to render. Render time increases with each component you add. Carefully measure the performance of your code components as you add more to a screen using the Developer Performance tools.\n\nCurrently, each code component bundles their own library of shared libraries such as Fluent UI and React. Loading multiple instances of the same library won't load these libraries multiple times. However, loading multiple different code components results in the browser loading multiple bundled versions of these libraries. In the future, these libraries will be able to be loaded and shared with code components.\n\n### Allow Makers to Style Your Code Component\n\nWhen app makers consume code components from inside a canvas app, they want to use a style that matches the rest of their app. Use input properties to provide customization options for theme elements such as color and size. When using Microsoft Fluent UI, map these properties to the theme elements provided by the library. In the future, theming support will be added to code components to make this process easier.\n\n### Follow Canvas Apps Performance Best Practices\n\nCanvas apps provide a wide set of best practices from inside the app and solution checker. Ensure your apps follow these recommendations before you add code components. For more information, see:\n\n- [Tips to improve canvas app performance](https://learn.microsoft.com/en-us/powerapps/maker/canvas-apps/performance-tips)\n- [Considerations for optimized performance in Power Apps](https://powerapps.microsoft.com/blog/considerations-for-optimized-performance-in-power-apps/)\n\n## TypeScript and JavaScript\n\n### ES5 vs ES6\n\nBy default, code components target ES5 to support older browsers. If you don't want to support these older browsers, you can change the target to ES6 inside your `pcfproj` folder's `tsconfig.json`. More information: [ES5 vs ES6](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/debugging-custom-controls#es5-vs-es6).\n\n### Module Imports\n\nAlways bundle the modules that are required as part of your code component instead of using scripts that are required to be loading using the `SCRIPT` tag. For example, if you wanted to use a non-Microsoft charting API where the sample shows adding `\u003cscript type=\"text/javascript\" src=\"somechartlibrary.js\u003e\u003c/script\u003e` to the page, this isn't supported inside a code component. Bundling all of the required modules isolates the code component from other libraries and also supports running in offline mode.\n\n\u003e **Note**: Support for shared libraries across components using library nodes in the component manifest is not yet supported.\n\n### Linting\n\nLinting is where a tool can scan the code for potential issues. The template used by `pac pcf init` installs the `eslint` module to your project and configures it by adding an `.eslintrc.json` file.\n\nTo configure, at the command-line use:\n\n```bash\nnpx eslint --init\n```\n\nThen answer the following questions when prompted:\n\n- **How would you like to use ESLint?** Answer: To check syntax, find problems, and enforce code style\n- **What type of modules does your project use?** Answer: JavaScript modules (import/export)\n- **Which framework does your project use?** Answer: React\n- **Does your project use TypeScript?** Answer: Yes\n- **Where does your code run?** Answer: Browser\n- **How would you like to define a style for your project?** Answer: Answer questions about your style\n- **What format do you want your config file to be in?** Answer: JSON\n- **What style of indentation do you use?** Answer: Spaces\n- **What quotes do you use for strings?** Answer: Single\n- **What line endings do you use?** Answer: Windows\n- **Do you require semicolons?** Answer: Yes\n\nBefore you can use `eslint`, you need to add some scripts to the `package.json`:\n\n```json\n\"scripts\": {\n   ...\n   \"lint\": \"eslint MY_CONTROL_NAME --ext .ts,.tsx\",\n   \"lint:fix\": \"npm run lint -- --fix\"\n}\n```\n\nNow at the command-line, you can use:\n\n```bash\nnpm run lint:fix\n```\n\nAdditionally, you can add files to ignore by adding to the `.eslintrc.json`:\n\n```json\n\"ignorePatterns\": [\"**/generated/*.ts\"]\n```\n\n## HTML Browser User Interface Development\n\n### Use Microsoft Fluent UI React\n\n[Fluent UI React](https://developer.microsoft.com/fluentui#/get-started/web) is the official [open source](https://github.com/microsoft/fluentui) React front-end framework designed to build experiences that fit seamlessly into a broad range of Microsoft products. Power Apps itself uses Fluent UI, meaning you are able to create UI that's consistent with the rest of your apps.\n\n#### Use Path-Based Imports from Fluent to Reduce Bundle Size\n\nCurrently, the code component templates used with `pac pcf init` won't use tree-shaking, which is the process where `webpack` detects modules imported that aren't used and removes them. If you import from Fluent UI using the following command, it imports and bundles the entire library:\n\n```typescript\nimport { Button } from '@fluentui/react'\n```\n\nTo avoid importing and bundling the entire library, you can use path-based imports where the specific library component is imported using the explicit path:\n\n```typescript\nimport { Button } from '@fluentui/react/lib/Button';\n```\n\nUsing the specific path reduces your bundle size both in development and release builds.\n\n#### Optimize React Rendering\n\nWhen using React, follow React specific best practices regarding minimizing rendering of components:\n\n- Only make a call to `ReactDOM.render` inside the `updateView` method when a bound property or framework aspect change requires the UI to reflect the change. You can use `updatedProperties` to determine what has changed.\n- Use `PureComponent` (with class components) or `React.memo` (with function components) where possible to avoid unnecessary re-renders.\n- For large React components, deconstruct your UI into smaller components to improve performance.\n- Avoid use of arrow functions and function binding inside the render function as these practices create a new callback closure with each render.\n\n### Check Accessibility\n\nEnsure that code components are accessible so that keyboard only and screen-reader users can use them:\n\n- Provide keyboard navigation alternatives to mouse/touch events\n- Ensure that `alt` and [ARIA](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA) (Accessible Rich Internet Applications) attributes are set so that screen readers announce an accurate representation of the code components interface\n- Modern browser developer tools offer helpful ways to inspect accessibility\n\nMore information: [Create accessible canvas apps in Power Apps](https://learn.microsoft.com/en-us/powerapps/maker/canvas-apps/accessible-apps).\n\n### Always Use Asynchronous Network Calls\n\nWhen making network calls, never use a synchronous blocking request since this causes the app to stop responding and result in slow performance. More information: [Interact with HTTP and HTTPS resources asynchronously](https://learn.microsoft.com/en-us/powerapps/developer/model-driven-apps/best-practices/business-logic/interact-http-https-resources-asynchronously).\n\n### Write Code for Multiple Browsers\n\nModel-driven apps, canvas apps, and portals all support multiple browsers. Be sure to only use techniques that are supported on all modern browsers, and test with a representative set of browsers for your intended audience.\n\n- [Limits and configurations](https://learn.microsoft.com/en-us/powerapps/maker/canvas-apps/limits-and-config)\n- [Supported web browsers](https://learn.microsoft.com/en-us/power-platform/admin/supported-web-browsers-and-mobile-devices)\n- [Browsers used by office](https://learn.microsoft.com/en-us/office/dev/add-ins/concepts/browsers-used-by-office-web-add-ins)\n\n### Code Components Should Plan for Supporting Multiple Clients and Screen Formats\n\nCode components can be rendered in multiple clients (model-driven apps, canvas apps, portals) and screen formats (mobile, tablet, web).\n\n- Using `trackContainerResize` allows code components to respond to changes in the available width and height\n- Using `allocatedHeight` and `allocatedWidth` can be combined with `getFormFactor` to determine if the code component is running on a mobile, tablet, or web client\n- Implementing `setFullScreen` allows users to expand to use the entire available screen available where space is limited\n- If the code component can't provide a meaningful experience in the given container size, it should disable functionality appropriately and provide feedback to the user\n\n### Always Use Scoped CSS Rules\n\nWhen you implement styling to your code components using CSS, ensure that the CSS is scoped to your component using the automatically generated CSS classes applied to the container `DIV` element for your component. If your CSS is scoped globally, it might break the existing styling of the form or screen where the code component is rendered.\n\nFor example, if your namespace is `SampleNamespace` and your code component name is `LinearInputComponent`, you would add a custom CSS rule using:\n\n```css\n.SampleNamespace\\.LinearInputComponent rule-name\n```\n\n### Avoid Use of Web Storage Objects\n\nCode components shouldn't use the HTML web storage objects, like `window.localStorage` and `window.sessionStorage`, to store data. Data stored locally on the user's browser or mobile client isn't secure and not guaranteed to be available reliably.\n\n## ALM/Azure DevOps/GitHub\n\nSee the article on [Code component application lifecycle management (ALM)](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/code-components-alm) for best practices on code components with ALM/Azure DevOps/GitHub.\n\n## Related Articles\n\n- [What are code components](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/custom-controls-overview)\n- [Code components for canvas apps](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/component-framework-for-canvas-apps)\n- [Create and build a code component](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/create-custom-controls-using-pcf)\n- [Learn Power Apps component framework](https://learn.microsoft.com/en-us/training/paths/use-power-apps-component-framework)\n- [Use code components in Power Pages](https://learn.microsoft.com/en-us/power-apps/maker/portals/component-framework)\n","description":"Best practices and guidance for developing PCF code components","import":{"commit_sha":"541b7819d8c3545c6df122491af4fa1eae415779","imported_at":"2026-05-18T20:05:35Z","license_text":"MIT License\n\nCopyright GitHub, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","owner":"github","repo":"github/awesome-copilot","source_url":"https://github.com/github/awesome-copilot/blob/541b7819d8c3545c6df122491af4fa1eae415779/instructions/pcf-best-practices.instructions.md"},"manifest":{}},"content_hash":[82,252,96,15,13,126,74,216,52,221,146,55,125,8,196,254,6,233,112,87,32,84,237,87,109,59,33,166,173,197,65,104],"trust_level":"unsigned","yanked":false}
