Modules · Core

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

NameTypeDescription
abooleanFirst operand, reactive or plain.
bbooleanSecond 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);
Combining two conditions with And

Logic.If

Runs a block while a condition is true.

Parameters

NameTypeDescription
conditionbooleanWhen true, the block runs.
onTruefunctionThe block to run.

Example

const agreed = Reactive.State(true);
Logic.If(agreed, () => {
  Reactive.Dom.text("Thanks!");
});
Showing content only while true

Logic.IfNot

Runs a block while a condition is false.

Parameters

NameTypeDescription
conditionbooleanWhen false, the block runs.
onFalsefunctionThe block to run.

Example

const loaded = Reactive.State(false);
Logic.IfNot(loaded, () => {
  Reactive.Dom.text("Loading…");
});
Running a block while false

Logic.Not

The negation of a condition.

Parameters

NameTypeDescription
conditionbooleanThe 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);
Negating a reactive boolean

Logic.Or

True when either input is true.

Parameters

NameTypeDescription
abooleanFirst operand, reactive or plain.
bbooleanSecond 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);
Granting access when either is true

Logic.Select

Runs one of two blocks by a condition.

Parameters

NameTypeDescription
conditionbooleanChooses which block runs.
onTruefunctionRuns when the condition is true.
onFalsefunctionRuns when the condition is false.

Example

const online = Reactive.State(true);
Logic.Select(online,
  () => Reactive.Dom.text("Online"),
  () => Reactive.Dom.text("Offline"));
Choosing between two blocks

Logic.SelectValue

Picks one of two values by a condition.

Parameters

NameTypeDescription
conditionbooleanWhich value to choose.
trueValueanyReturned when the condition is true.
falseValueanyReturned 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");
Picking a value by condition