3 ms·
Nice, I do something similar in a web framework I've been working on. All rendering happen on the server so the callbacks has to run there too. The onclick-hand
by quechimba 4y ago
Nice, I do something similar in a web framework I've been working on. All rendering happen on the server so the callbacks has to run there too. The onclick-handler in the DOM will trigger a POST to the server with an unique callback id.
def self.get_initial_state(initial_count: 0, **)
{ count: initial_count }
end
def handle_decrement(_)
update do |state|
{ count: [0, state[:count] - 1].max }
end
end
def handle_increment(_)
update do |state|
{ count: state[:count] + 1 }
end
end
def render
<div class={styles.counter}>
<button on-click={handler(:handle_decrement)} disabled={state[:count].zero?}>
-
</button>
<span>{state[:count]}</span>
<button on-click={handler(:handle_increment)}>
+
</button>
</div>
end