🍏 iOS / Mac OSNew government liaison Tim Cook to dine with AI & world leaders(17.09.2026 um 02:10 Uhr)
🍏 iOS / Mac OSNew government liaison Tim Cook to dine with AI & world leaders(17.09.2026 um 02:10 Uhr)
🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Reusable, Extensible and Testable State Logic with Reactive Programming.

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




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:




CODE
<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:




CODE
<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




CODE
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 .




CODE
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.




CODE
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!

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
ZDI-26-713: GIMP APNG File Parsing Stack-based Buffer Overflow Remote Code Execution Vulnerability
1 Quelle
ZDI-26-712: NoMachine nxhtd Server-Side Request Forgery Information Disclosure Vulnerability
1 Quelle
ZDI-26-711: NoMachine Redis Improper Authentication Local Privilege Escalation Vulnerability
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Reusable, Extensible and Testable State Logic with Reactive Programming.

Thematisch verwandte Begriffe: Reusable, Extensible, Testable, State · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...