CxJS

Functional Components

import { createFunctionalComponent } from 'cx/ui'; Copied

Functional components provide a simple way to create reusable structures composed of CxJS widgets. Use createFunctionalComponent to define a CxJS functional component:

<div class="flex flex-col gap-4">
  <Counter value={m.count1} label="Counter A" />
  <Counter value={m.count2} label="Counter B" />
</div>
Counter A:0
Counter B:0

The Counter component can be reused with different props, each instance maintaining its own state through different store bindings.

Example 2

Functional components are useful for creating reusable chart configurations:

<div class="flex flex-col gap-4">
  <LineChart
    chartStyle="height: 200px; background: white; border: 1px solid lightgray"
    lineStyle="stroke: red; stroke-width: 1"
    areaStyle="fill: rgba(255, 0, 0, 0.3)"
    data={Array.from({ length: 50 }, (_, x) => ({
      x,
      y: 75 - 50 * Math.random(),
    }))}
  />
  <LineChart
    chartStyle="height: 200px; background: #1a1a2e; color: gray;"
    lineStyle="stroke: lightgreen; stroke-width: 2"
    data={Array.from({ length: 50 }, (_, x) => ({
      x,
      y: 75 - 50 * Math.random(),
    }))}
  />
  <LineChart
    chartStyle="height: 200px; background: #2e2e4c; color: rgba(255, 255, 255, 0.7);"
    lineStyle="stroke: currentColor; stroke-width: 2"
    data={Array.from({ length: 50 }, (_, x) => ({
      x,
      y: 75 - 50 * Math.random(),
    }))}
  />
</div>

The same LineChart component renders three different chart styles by passing different props.

Conditional Logic

Functional components can contain conditional logic:

<div class="flex gap-8 items-center">
  <MyForm />
  <MyForm vertical />
</div>

The vertical prop changes the layout at render time.

Reserved Properties

These properties are handled by the framework and should not be used inside the function body:

PropertyTypeDescription
visiblebooleanIf false, the component won’t render and its controller won’t initialize. Alias: if.
controllerControllerController that will be initialized with the component.
layoutLayoutInner layout applied to child elements.
outerLayoutLayoutOuter layout that wraps the component.
putIntostringContent placeholder name for outer layouts. Alias: contentFor.