Logic
Booleans and conditional logic. Logic gives you reactive and, or and
not over plain JavaScript booleans, plus conditional blocks that run only while a
condition holds. Every result is an ordinary reactive value, so a condition stays live and
re-evaluates whenever the values it depends on change.
Contents
Functions
Functions
Logic.And
True when both inputs are true.
Parameters
| Name | Type | Description |
|---|---|---|
a | boolean | First operand, reactive or plain. |
b | boolean | Second operand, reactive or plain. |
Returns
A reactive boolean, true only when both a and b are true.
Example
const ready = Reactive.State(true);
const willing = Reactive.State(false);
const go = Logic.And(ready, willing);Logic.If
Runs a block while a condition is true.
Parameters
| Name | Type | Description |
|---|---|---|
condition | boolean | When true, the block runs. |
onTrue | function | The block to run. |
Example
const agreed = Reactive.State(true);
Logic.If(agreed, () => {
Reactive.Dom.text("Thanks!");
});Logic.IfNot
Runs a block while a condition is false.
Parameters
| Name | Type | Description |
|---|---|---|
condition | boolean | When false, the block runs. |
onFalse | function | The block to run. |
Example
const loaded = Reactive.State(false);
Logic.IfNot(loaded, () => {
Reactive.Dom.text("Loading…");
});Logic.Not
The negation of a condition.
Parameters
| Name | Type | Description |
|---|---|---|
condition | boolean | The value to negate. |
Returns
A reactive boolean, false when condition is true and true when it is false.
Example
const loading = Reactive.State(true);
const ready = Logic.Not(loading);Logic.Or
True when either input is true.
Parameters
| Name | Type | Description |
|---|---|---|
a | boolean | First operand, reactive or plain. |
b | boolean | Second operand, reactive or plain. |
Returns
A reactive boolean, true when a or b (or both) are true.
Example
const admin = Reactive.State(false);
const owner = Reactive.State(true);
const canEdit = Logic.Or(admin, owner);Logic.Select
Runs one of two blocks by a condition.
Parameters
| Name | Type | Description |
|---|---|---|
condition | boolean | Chooses which block runs. |
onTrue | function | Runs when the condition is true. |
onFalse | function | Runs when the condition is false. |
Example
const online = Reactive.State(true);
Logic.Select(online,
() => Reactive.Dom.text("Online"),
() => Reactive.Dom.text("Offline"));Logic.SelectValue
Picks one of two values by a condition.
Parameters
| Name | Type | Description |
|---|---|---|
condition | boolean | Which value to choose. |
trueValue | any | Returned when the condition is true. |
falseValue | any | Returned when the condition is false. |
Returns
The chosen value as a reactive value, either trueValue or falseValue.
Example
const dark = Reactive.State(true);
const theme = Logic.SelectValue(dark, "midnight", "daylight");