# Write a custom jComponent

> Register a reusable component with COMPONENT(), render it in make(), react to state changes in setter() and write back with set().

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: en
Content-SHA256: e7cce28ba59dc0aeba03d3e4d38b42f387d4ab3717e831f55440d79e4914233b
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: jcomponent20@3fedd009a277f50fddc608b97afbec553b67c51b
Review: not-recorded

A component is declared with `COMPONENT(name, config, declaration)`. The second argument is the default configuration as a string (`key:value;key:value`). The declaration receives `self` (the component instance) and `config` (the parsed configuration).

- `self.make` runs once, when the component is created. Build the DOM here.
- `self.setter(value)` runs every time the value of the component's `path` changes.
- `self.set(value)` writes a new value to the `path`; `self.get()` reads it.

Use the component with a `<ui-component>` element. `name` selects the declaration, `path` the state and `config` overrides the defaults.

```html public/index.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>jComponent</title>
	<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
	<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>

	<ui-component name="counter" path="cart.count" config="step:2"></ui-component>
	<p>Total: <ui-bind path="cart.count" config="text:value"></ui-bind></p>

	<script>

		COMPONENT('counter', 'step:1', function(self, config) {

			self.make = function() {
				self.html('<button type="button">Add</button> <b class="value">0</b>');
				self.event('click', 'button', function() {
					self.set((self.get() || 0) + config.step);
				});
			};

			self.setter = function(value) {
				self.find('.value').text(value || 0);
			};

		});

		SET('cart.count', 10);

	</script>

</body>
</html>
```

The `ui-bind` next to the component shows that both read the same state: clicking the button changes the path and every element bound to it updates.
