@Prop() first: string;
@Prop() middle: string;
@Prop() last: string;
The quick idea is that our render function needs to return a representation of the HTML we want to push to the DOM.
private getText(): string {
return format(this.first, this.middle, this.last);
}
render() {
return <div>Hello, World! I'm {this.getText()}</div>;
}
This component's render() returns a <div> element, containing text to render to the screen.
The render() function uses all three class members decorated with @Prop(), through the getText function.
Declaring private functions like getText helps pull logic out of the render() function's JSX.
Any property decorated with @Prop() is also automatically watched for changes.
If a user of our component were to change the element's first, middle, or last properties, our component would fire its render() function again, updating the displayed content.
Local Development
After creating your Stencil components, you'll likely want to use them in an existing application. There are multiple approaches for local development depending on your project setup.
Framework Integration
If you want to integrate your Stencil components directly into an existing application built with frameworks like React, Angular, or Vue, refer to the Framework Integrations guide for specific integration instructions.
Using Component Library in Another Project
If you're building a standalone component library and want to use it in another project during development, you have two main options:
For applications that don't use npm or for simple HTML pages, you can include your components directly with a script tag:
cd my-first-stencil-project
npm run build
This creates a dist folder containing your compiled components. Copy this folder to your application, then add a script tag that points to the ESM bundle:
<!DOCTYPE html>
<html>
<head>
<script type="module" src="path/to/dist/my-first-stencil-project/my-first-stencil-project.esm.js"></script>
</head>
<body>
<my-component first="Stencil" middle="'Don't call me a framework'" last="JS"></my-component>
</body>
</html>
Note: When using script tags, your application must be served from a web server rather than opened as a local file. You can use tools like http-server or your IDE's built-in server.
When you update your Stencil components, remember to rebuild the project and update the files in your consuming application.
Both approaches allow you to develop and test your components in the context of a real application, making it easier to refine their design and functionality.
Using npm link
For npm-based projects, npm link creates a symbolic link between your Stencil component library and the consuming application. However, linking to a Stencil component this way can still be a little tricky. Angular, React, and Vue each have their own documentation which includes npm link and it's recommended you follow those integration guides.
Updating Stencil
To get the latest version of @stencil/core you can run:
npm install @stencil/core@latest --save-exact
yarn add @stencil/core@latest --exact
pnpm add @stencil/core@latest --save-exact