UNPKG

926 BMarkdownView Raw
1## Headless Components
2
3Components can also be registered as headless (without view). These are components that live in other components as contained mixins and handle logic, events and rendering. This is useful for plugins that handle global data and some logic.
4
5```js
6import { action, headless, Component } from 'radi';
7
8class GlobalComponent extends Component {
9 state() {
10 return {
11 count: 0
12 }
13 }
14
15 @action tick() {
16 return {
17 count: this.state.count + 1
18 }
19 }
20
21 on() {
22 return {
23 mount() {
24 setInterval(() => {
25 this.tick()
26 })
27 }
28 }
29 }
30}
31
32headless('myGlobalComponent', GlobalComponent)
33```
34
35Now that we registered headless component it can be accessed by every component with dollar sign + handle name `this.$myGlobalComponent`.
36
37```jsx
38<h1>{ listen(this.$myGlobalComponent, 'count') }</h1>
39```
40
41This will output `GlobalComponent` state.count output.