Skip to content
On this page

Functions

Once you have some cells, you can compute values based on those cells using normal functions.

tsx
import { Cell } from "@starbeam/universal";
 
const name = Cell("John");
const location = Cell("New York");
 
function description() {
return `${name.current} lives in ${location.current}`;
}
tsx
import { Cell } from "@starbeam/universal";
 
const name = Cell("John");
const location = Cell("New York");
 
function description() {
return `${name.current} lives in ${location.current}`;
}
tsx
import { Cell } from "@starbeam/universal";
 
const name = Cell("John");
const location = Cell("New York");
 
function description(): string {
return `${name.current} lives in ${location.current}`;
}
tsx
import { Cell } from "@starbeam/universal";
 
const name = Cell("John");
const location = Cell("New York");
 
function description(): string {
return `${name.current} lives in ${location.current}`;
}

And then use the function as normal.

tsx
description();
// "John lives in New York"
 
name.set("John Doe");
description();
// "John Doe lives in New York"
 
location.set("Los Angeles");
description(); // "John Doe lives in Los Angeles"
tsx
description();
// "John lives in New York"
 
name.set("John Doe");
description();
// "John Doe lives in New York"
 
location.set("Los Angeles");
description(); // "John Doe lives in Los Angeles"

You can also pass cells into functions as arguments.

tsx
function description(name) {
return `${name.current} lives in ${location.current}`;
}
tsx
function description(name) {
return `${name.current} lives in ${location.current}`;
}
tsx
function description(name: Cell<string>): string {
return `${name.current} lives in ${location.current}`;
}
tsx
function description(name: Cell<string>): string {
return `${name.current} lives in ${location.current}`;
}

And then use the function as normal.

tsx
description(name); //=> "John lives in New York"
 
name.set("John Doe");
description(name); //=> "John Doe lives in New York"
 
location.set("Los Angeles");
description(name); //=> "John Doe lives in Los Angeles"
tsx
description(name); //=> "John lives in New York"
 
name.set("John Doe");
description(name); //=> "John Doe lives in New York"
 
location.set("Los Angeles");
description(name); //=> "John Doe lives in Los Angeles"

Released under the MIT license