Ink provides several ways to manage properties and states
in your components.
Props
import { props } from '@stackpress/ink';
const { title, description } = props();
The props function can be used to access the
properties of a component.
Signals
Ink provides a reactive state management system that allows
you to manage states in your components. The system is based
on signals, which are reactive variables that can be used to
store and update data. Signals can be used to store any type
of data, including numbers, strings, objects, arrays, and even
functions.
<script>
import { signal } from '@stackpress/ink';
const count = signal<number>(1);
</script>
<em class=classlist>Count #{count.value}</em>
To create a signal, you can use the
signal()
function, which takes an initial value as an argument. Signals
can be read and updated using the value property.
Setting the value will trigger a re-render of the component.
Signals can be used in your components to manage states and
trigger updates when the state changes. You can use signals to
store data that needs to be shared between components, or to
trigger side effects when the state changes. Signals can also
be used to store data that needs to be persisted across page
reloads, such as form data or user preferences.
For example, you can use the click
attribute assigned to a function to trigger a function when
the element is clicked. In combination with updating a signal,
can trigger a re-render of the component. The following event
attributes are supported.
For other edge cases, the component function
can be used to get raw access to the component's
functionality.
Environment Variables
<script>
import { env } from '@stackpress/ink';
const { BUILD_ID, NODE_ENV } = env();
</script>
<if true={NODE_ENV === 'development'}>
<p>Development mode</p>
</if>
The env function can be used to access environment
variables in a component.
this
<script>
this.props;
this.style;
this.classList;
this.parentNode;
this.innerHTML;
this.appendChild();
this.querySelector('p');
</script>
this refers to the
InkComponent that extends
HTMLElement. This means all
components in Ink are in fact are HTML elements and has
access to the common functionality like
innerHTML and
querySelector() to name a
few. InkComponent has the
additional following properties and methods that you can access
using this.
Info: You can discover more methods and properties
of the HTMLElement class on the
MDN Web Docs
.