85
edits
No edit summary |
No edit summary |
||
Line 17: | Line 17: | ||
Since I want to side-step the modern frontend stack a bit, I found Preact as an alternative implementation of React in 3kb, and it [https://dev.to/jovidecroock/prefresh-fast-refresh-for-preact-26kg has a blog post] describing how to hook into its hot updating system. I quickly whipped up a macro which implements a JSX-like syntax, and a <code>defcomponent</code> macro for creating React components, so I can write something like this: | Since I want to side-step the modern frontend stack a bit, I found Preact as an alternative implementation of React in 3kb, and it [https://dev.to/jovidecroock/prefresh-fast-refresh-for-preact-26kg has a blog post] describing how to hook into its hot updating system. I quickly whipped up a macro which implements a JSX-like syntax, and a <code>defcomponent</code> macro for creating React components, so I can write something like this: | ||
<syntaxhighlight lang="lisp">(defcomponent - | <syntaxhighlight lang="lisp"> | ||
(defcomponent -counter (((counter set-counter) (use-state 0))) () | |||
(psx | |||
(:div () | |||
"The button has been clicked " | |||
(:span ((style (create "color" "red"))) | |||
(chain counter (to-string))) | |||
" times." | |||
(:button ((onclick (lambda () (set-counter (1+ counter))))) | |||
"Increment")))) | |||
</syntaxhighlight> | |||
[[File:Clicked.png]] | |||
and have it compile into: | and have it compile into: | ||
Line 32: | Line 35: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
var s = $RefreshSig$(); | var s = $RefreshSig$(); | ||
function | function Counter() { | ||
s(); | s(); | ||
var | var _db1107 = preactHooks.useState(0); | ||
var counter = _db1107[0]; | |||
var setCounter = _db1107[1]; | |||
__PS_MV_REG = []; | __PS_MV_REG = []; | ||
return [preact.h('div', Object.assign({ | return [preact.h('div', Object.assign({ }), ['The button has been clicked '], [preact.h('span', Object.assign({ style : { 'color' : 'red' } }), [counter.toString()])], [' times.'], [preact.h('button', Object.assign({ onclick : function () { | ||
__PS_MV_REG = []; | |||
return setCounter(counter + 1); | |||
} }), ['Increment'])])]; | |||
}; | }; | ||
s( | s(Counter, 'useState{[counter, setCounter](0)}', false, function () { | ||
return []; | return []; | ||
}); | }); | ||
$RefreshReg$( | $RefreshReg$(Counter, 'Counter'); | ||
flushUpdates(); | flushUpdates(); | ||
</syntaxhighlight> | </syntaxhighlight> |