-
Notifications
You must be signed in to change notification settings - Fork 104
Description
The tutorials are good, but there are few points that aren't clear and that could be improved, IMHO.
For example:
- How do I add multiple (reactive) node without a parent node
- How do I create a (reactive) node those presence depends on a signal / reactive value
- How the data flow into the system (and when)
For 1, I'm doing this, don't know if it's what recommended:
const Func = () => {
return [span("A"), span("list"), span("of"), span("nodes")]
}
van.add(document.body, Func())
Tutorials always show this, but it's not required
const Func = () => {
return div( // <= Useless container
span("A"), span("list"), span("of"), span("nodes"))
}
van.add(document.body, Func())
Yet, sometimes returning an array doesn't work (see point 2 below)
For 2, I find it's extremely hard to achieve in the current state of the library. For example, this doesn't work
let a = van.state(0)
// Doesn't work as expected, won't be changed when a.val is modified
const DerivedState = () => {
if (a.val)
return [span("A"), span("list"), span("of"), span("nodes")]
else
return [span("A"), span("list"), span("nodes")]
}
van.add(document.body, DerivedState())
a.val = 1
The workaround is like this:
let a = van.state(0)
// An effect must be created here else, it isn't reflected in the DOM
const der = van.derive(() => a.val ? div(span("A"), span("list"), span("of"), span("nodes")) : div(span("A"), span("list"), span("nodes")))
// This doesn't work either, so we're back to the useless container
//const der = van.derive(() => a.val ? [span("A"), span("list"), span("of"), span("nodes")] : [span("A"), span("list"), span("nodes"))]
van.add(document.body, der)
// Trigger change to check if DOM is modified
a.val = 1
BTW, I know that in the former case, the function DerivedState()
is called once and can't be recalled on reactive change thus it can be changed to ()=>DerivedState()
or simply DerivedState
, but it's very hard to understand or spot the error. At least the documentation should list it.
Is it possible to detect, in debug build, that the arguments to van.add
are reactive but aren't a function AND are a van.tags making use of a signal
, and thus output an error on the console? I couldn't think of a case where these conditions would work out well.
Something like this (very poor man detection method of a reactive signal call inside a function):
let globalReactiveFound = 0
class AProx // Mimic state interface
{
constructor(e = 0) { this.e = e }
set val(e) { this.e = e; globalReactiveFound = 1 }
get val() { globalReactiveFound = 1; return this.e }
}
var b = new AProx(0)
function something() {
console.log(b.val); // Reactive access
}
const va = {
add: function(e) { if (globalReactiveFound) console.error("e is reactive but won't react to change"); globalReactiveFound = 0 }
}
va.add(something()) // Here, calling the function will change the flag thus is detectable in va.add