Create a Component
The Geocortex Web SDK project template includes an example of a custom component following our best practices. Creating a new custom component involves a few important steps:
- Create the component source file
- Create the component model
- Register the component and component model in your library registration file
Create the Component
Create the component source file in a new folder within the src/components
folder. For example to create a new component called ExampleComponent
, create a new file called ExampleComponent.tsx
in src/components/ExampleComponent
with the following content:
Create the Component Model
Next we need to create the component model that will be bound to the component we just created. This model will function as the underlying data source for the component.
tip
It's best practice to use the model to define configurable or persistent state, and use the React state hooks for UI specific transient state, like an active selection.
Create a new file called ExampleComponentModel.ts
in src/components/ExampleComponent
:
Register the Component
Finally we need to register the component and component model with the Geocortex Web component registry so that it is aware of your new component.
To simplify the module imports, we'll make a new file in the ExampleComponent
folder called index.ts
. Note that this file is for convenience, and is not required:
Now register your component and component model with the component registry by modifying the src/index.ts
file:
Add the Component to your Layout
You can now add the component to your layout.xml
file and run the development server to see it in use.
- Layout
- UI
Next Steps
Check out the Component Reference
Take a deep dive into components in the Geocortex Web SDK
Deploy your Component
Learn how to deploy your custom component.
Create a Component with a Complex UI
Follow along with a more in depth component example
Create a Component that Consumes App Config
Learn more about writing components that consume configuration values.