-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
Before we start, install Hywer
+ Vite
template first
git clone https://github.com/ssleert/hywer-vite-template.git ./hywer-test
cd ./hywer-test
npm i
npm run dev
const App = () => {
return <>
<p>👋Hello</p>
<ul>
<li>🗺️World</li>
<li>
<a href="https://github.com/ssleert/hywer">
Hywer🥢
</a>
</li>
</ul>
</>
}
document.getElementById("app").append(...<><App /></>)
-
The code should be self-explanatory if you are already familiar with plain
js
andhtml
. This code is written using JSX which allows us to create Elements using a clearXML
-like syntax. -
Using
JSX
in most cases requires first translating the code into vanilla js. This is usually done with tools likebabel
,esbuild
orwebpack
. -
Unlike
React
andVue
, Hywer does not add a Virtual DOM layer between yourJSX
elements and the realDOM
. Your<p>
is a realHTMLParagraphElement
that you can interact with using native browserAPI
.
import { ref } from "hywer"
const App = () => {
const text = ref("")
return <>
<input value={text} onInput={e => text.val = e.target.value} />
<input value={text} onInput={e => text.val = e.target.value} />
</>
}
document.getElementById("app").append(...<><App /></>)
-
ref(value)
provides an observer forvalue
also usually calledReactive Values
. That can be connected toDOM
. And reactively update it value inDOM
Node orElement
attribute (asvalue={text}
here). -
⚠️ While you can updateReactive Values
by setting the val property, you should never mutate the underlying object ofval
itself. Doing so will not trigger theDOM
tree update as you would expect and might result in undefined behavior due to aliasing. -
on...
attributes provides as a convenient interface foraddEventListener()
. Afteron
you can write any event name in any case, but camelCase is preferred (likeonClick
,onInput
,onBeforeToggle
,onPaste
etc).
import { ref, derive } from "hywer"
const App = () => {
const elapsed = ref(0)
let id = null
const start = () => {
id = (id == null)
? setInterval(() => elapsed.val += 0.01, 10)
: null
}
const elapsedString = elapsed.derive(val => val.toFixed(2) + "s ")
// you can also write like this if u want to derive from multiple values:
//
// const elapsedString = derive(refs => {
// return refs[0].val.toFixed(2) + "s "
// }, [elapsed])
return <>
<pre style="display: inline;">
{elapsedString}
</pre>
<button onClick={start}>Start</button>
<button onClick={() => { clearInterval(id); id = null }}>
Stop
</button>
<button onClick={() => elapsed.val = 0}>Reset</button>
</>
}
document.getElementById("app").append(...<><App /></>)
-
value.derive(fn)
orderive(fn, refs)
can help in such cases when you want to make another reactive value that updates when it parent or parents updates.
import { ref, effect } from "hywer"
const App = () => {
const count = ref(0)
count.sub = (val, oldVal) => {
console.log(`count.val = ${val}; count.oldVal = ${oldVal}`)
}
// you can also write like this if u want to make effect for multiple values:
// effect(([count]) => {
// console.log(`count.val = ${count.val}; count.oldVal = ${count.oldVal}`)
// }, [count])
return <>
❤️ {count}
<button onClick={() => count.val++}>👍</button>
<button onClick={() => count.val--}>👎</button>
</>
}
document.getElementById("app").append(...<><App /></>)
-
set value.sub
oreffect(fn, refs)
can help when you want to do a side-effect on a value change without connecting the derived value to the DOM -
⚠️ Do not addvalues.sub
oreffect(fn, refs)
on variables from the parent scope in code that may be run more than once. This can lead to memory leaks because lambdas have different references for each creation andSet
of subscribers will eventually overflow because gc can't figure out which subscribers to clear.
This was a short introductory course on Hywer.
-
I highly recommend figuring out how Hywer works under the hood, the code is quite documented and easy to understand. Code Link
-
Sample applications.
- 🚧 Work in progress.