rebloom exports the getUse
function that turns any object into a use-able object whose properties are listened to by the use
property hook inside React components.
// appState.ts
import { getUse } from 'rebloom';
class AppState {
// define "use"
readonly use = getUse<AppState>();
// define other properties
count = 0;
ids: number[] = [];
// define methods
readonly increment = () => this.count++;
readonly decrement = () => this.count--;
}
// instantiate the class and export the instance
const appState = new AppState();
export default appState;
// MyComponent.ts
import appState from './appState';
const MyComponent = () => {
// the component is re-rendered when 'count' or 'ids' are reassigned
const count = appState.use('count');
const ids = appState.use('ids');
return (
<div onClick={() => appState.count++}>Clicks: {count}</div>
)
}
export default MyComponent;
A use-able object is a regular object, and all its properties and methods are used in the traditional way. Properties need to maintain immutability to invoke the property accessor. The readonly
prefix protects properties from being reassigned.
appState.count++;
appState.ids = [...state.users.ids, 4];
appState.increment();
appState.increment = () => {}; // error because of the "readonly" prefix
// appState.ts
import { type WithUse, getUse } from 'rebloom';
type AppState = WithUse<{
count: number;
ids: number[];
}>;
const appState: AppState = {
use: getUse(),
count: 0,
ids: [],
}
Use the same principle as with an instantiable class, but all class members are defined with the static
prefix, and the class isn't instantiated but used directly as an object.
// appState.ts
import { getUse } from 'rebloom';
export default class AppState {
// define "use"
static readonly use = getUse<typeof AppState>();
// define other properties
static count = 0;
static ids: number[] = [];
// define methods
static readonly increment = () => this.count++;
static readonly decrement = () => this.count--;
}
// MyComponent.ts
import AppState from './appState';
const MyComponent = () => {
const count = AppState.use('count');
const ids = AppState.use('ids');
return (
<div onClick={() => AppState.count++}>Clicks: {count}</div>
)
}
export default MyComponent;
That's not it. More docs are coming soon.