Building Custom JavaScript Components
This document outlines how to build custom Universal app components.
Universal is extensible and you can build custom JavaScript components and frameworks. This document will cover how to build custom components that integrate with the Universal app platform.
This is an advanced topic and not required if you simply want to use Universal Apps.
Step-By-Step
This following section will take you step-by-step through the different aspects of building a Universal App component.
1. Installing Dependencies
You will need to install the following dependencies before creating your component.
2. Install PowerShellUniversal.Apps.Tools Module
The PowerShellUniversal.Apps.Tools
module is available on the PowerShell Gallery.
3. Create a New Project
Use the New-UDReactComponent
cmdlet to create a new project. This will scaffold the project and all the necessary files.
4. Install JavaScript Packages
You can now install the JavaScript React packages you want to use in your component. For example, you could install react-icons
.
5. Update Your Project
Now you will need to author the code to use the React component. You will need to update the PSM1 and JSX file to author the logic for your component.
For example, the PSM1 would load the JavaScript file into the PSU AssetService. It would then define functions to load the JavaScript component and pass in properties.
In the component's JSX file, update the JavaScript to handle the properties passed to the component from PowerShell.
6. Build The Project
You can now build your project. It will output a module you can load into PowerShell Universal.
6. Use in PowerShell Universal
Within your app, load your module and execute the function.
Props
Props are values that are either passed from the PowerShell hashtable provided by the user or by the Universal App withComponentsFeature
high-order function.
Standard
The properties that you set in your hashtable in PowerShell will automatically be sent in as props to React component.
For example, if you set the text
property of the hashtable like this.
Then you will have access to that prop in React.
Endpoints
Endpoints are special in the way they are registered and the way that they are passed as props to your component. You will need to call Register
on the endpoint in PowerShell and pass in the Id and PSCmdlet variables.
Endpoints are created from ScriptBlocks and are executed when that event happens.
Universal will automatically wire up the endpoint to a function within JavaScript. This means that you can use the props to call that endpoint.
Notice the props.onClick
function call. This will automatically call the PowerShell script block on the server.
setState
The setState
prop is used to set the state of the component. This ensures that the state is tracked and your component will work with Get-UDElement
.
For example, with a text field, you'll want to call props.setState
and pass in the new text value for the state.
children
The children
prop is a standard React prop. If your component supports child items, such as a list or select box, you should use the standard props.children
prop to ensure that the cmdlets Add-UDElement
, Remove-UDElement
and Clear-UDElement
function correctly.
Last updated