Replies: 2 comments 3 replies
-
In this example, I want to demonstrate a few similarities and some differences between concepts in IRP (Imperative (Procedural) Reactive Programming) as we have with Subscript Functions and FRP (Functional Reactive Programming) as we have with Frameworks like React, Solid JS, and similar. Let's start by comparing the meaning of the word "function" in both programming paradigms.
So, it is easy to think
|
Beta Was this translation helpful? Give feedback.
-
The great thing about Subscript Functions is that it lets us drop a reactive programming context into normal JS code - offering us a block of contiguous lines of code where reactivity is self-managed. Below is a mental model: Now, being a self-managed "sub-script", code in the main script need not worry what's happening within. But there's a window into the reactive context that allows changes in the parent context to be propagated in to update dependent expressions: the And that's about Subscript Functions! Now here's an example of a custom element having it's // Outer dependency
let count = 10; customElements.define( 'click-counter', class extends HTMLElement {
connectedCallback() {
// Full rendering at connected time
// The querySelector() calls below are run
this.render();
// Fine-grained rendering at click time
// The querySelector() calls below don't run again
this.addEventListener( 'click', () => {
count ++;
this.render.thread( [ 'count' ] );
} );
}
**render() {
let countElement = document.querySelector( '#count' );
countElement.innerHTML = count;
let doubleCount = count * 2;
let doubleCountElement = document.querySelector( '#double-count' );
doubleCountElement.innerHTML = doubleCount;
let quadCount = doubleCount * 2;
let quadCountElement = document.querySelector( '#quad-count' );
quadCountElement.innerHTML = quadCount;
}
} ); Above, |
Beta Was this translation helpful? Give feedback.
-
Coming to Subscript Functions from a totally different background? Wondering how patterns compare between Subscript Functions and your framework of choice?
Questions are welcome in the comments! And it's fine to ask in a new topic.
Beta Was this translation helpful? Give feedback.
All reactions