State in UI Components
When we start introducing state into a UI component it is often tempting to do something like this:
Simple Counter Example in Svelte:
<script>
let count = $state(0);
</script>
<button onclick={() => count++}>
clicks: {count}
</button>
Problem
We use Svelte here but same the issue can apply in any UI framework.
Off the bat we have tightly coupled our state logic with the component - the increment functionality is coded right in the template.
With this set up we can not reuse the counter logic in:
- other UI components
- other business logic that requires a counter
- other UI frameworks
We also cannot test the counter logic without rendering the component. If the component has other functionality coupled with it, isolating the counter logic would not be possible.
Solution:
Separate state logic from the UI component so it can be implemented and tested independently. This will make the logic more reusable and UI components only need to listen for state changes and render. Consider the following:
<script>
import { RxCounter } from './RxCounter';
const [count, { increment }] = RxCounter();
</script>
<button onclick={increment}>
Count: {$count}
</button>
See full example on that emits the new count state. increment is an action method the UI can call to invoke state changes.
Svelte has a nice built in way of subscribing to observables in the template as seen with the $count template variable. Subscribing to an observable in any UI framework is not terribly difficult, and many frameworks have APIs to help accomplish this.
Implementing RxCounter
Information and Disclaimer: We will make use of to make reactive programming easier.
Get the Reactables Core Package and create RxCounter.
npm i @reactables/core
import { RxBuilder } from '@reactables/core';
export const RxCounter = () =>
RxBuilder({
initialState: 0,
reducers: {
increment: (count) => count + 1,
},
});
.
A emitting state changes and second item being a dictionary of action methods for triggering state updates.
The state is reactive, meaning the state responds to changes via its reducer functions. When an increment action triggered by the UI is received, the reactable reacts and its observable emits the new state.
Note RxCounter is now completely decoupled from the view layer and can be used in any UI framework!
Testing the Counter
To test a .
import { RxCounter } from './RxCounter';
import { Subscription } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
describe('RxCounter', () => {
let testScheduler: TestScheduler;
let subscription: Subscription;
beforeEach(() => {
testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
});
afterEach(() => {
subscription?.unsubscribe();
});
it('should increment', () => {
testScheduler.run(({ expectObservable, cold }) => {
// Create Counter Reactable
const [count$, { increment }] = RxCounter();
// Call actions
subscription = cold('--b', {
b: increment,
}).subscribe((action) => action());
// Assertions
expectObservable(count$).toBe('a-b', {
a: 0,
b: 1,
});
});
});
});
Extending the Counter
With the state logic isolated from the view, we can also extend the functionality of RxCounter and create a RxDoubleCounter that has the ability to double the count.
import { RxBuilder } from '@reactables/core';
export const RxCounter = (options) =>
RxBuilder({
initialState: 0,
reducers: {
increment: (count) => count + 1,
...options?.reducers,
},
});
export const RxDoubleCounter = () =>
RxCounter({
reducers: { doubleCount: (count) => count * 2 },
});
See full example on to learn more!
SOCIAL SHARE CARD GENERATOR