Best Practices and Guidance For Cod
Best Practices and Guidance For Cod
framework
Article
03/26/2024
10 contributors
In this article
Power Apps component framework
Model-driven apps
Canvas apps
TypeScript and JavaScript
Show 3 more
Developing, deploying, and maintaining code components needs a combination of
knowledge that includes the following areas:
Model-driven apps
This section contains best practices and guidance relating to code components
within model-driven apps.
A 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.
Canvas apps
This section contains best practices and guidance relating to code components
within canvas apps.
Currently, 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.
ES5 vs ES6
By 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.
Module imports
Always 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
<script type="text/javascript" src="somechartlibrary.js></script> 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.
Note
Support for shared libraries across components using library nodes in the component
manifest is not yet supported.
Linting
Linting 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. Eslint requires configuring for TypeScript and React
coding styles. It can also be used to fix some of these issues automatically where
possible. To configure, at the command-line use:
shell
Copy
npx eslint --init
Then answer the following questions when prompted:
How would you like to use ESLint? Answer: To check syntax, find problems, and
enforce code style
What type of modules does your project use? Answer: JavaScript modules
(import/export)
How would you like to define a style for your project? Answer: Answer questions
about your style
What format do you want your config file to be in? Answer: JSON (This answer
updates the existing .eslintrc.json)
What style of indentation do you use? Answer: Spaces (This indentation style is the
Visual Studio Code default)
What line endings do you use? Answer: Windows (This line ending is the Visual
Studio Code default CRLF line endings style.)