-
I came across this code snipplet in the example [State objects as properties](https://vanjs.org/tutorial#state-typed-prop) const value = van.state("")
return span(
input({type: "text", value, oninput: e => value.val = e.target.value}),
input({type: "text", value, oninput: e => value.val = e.target.value}),
)
} This shows state propagation from one input to the other. So, one is acting as a source and one as a target. What happens here?
I was wandering, if this has any effect or if there are any benefits on using different states like this: const text1 = van.state("")
const text2 = van.state("")
return span(
input({type: "text", value: text1, oninput: e => text2.val = e.target.value}),
input({type: "text", value: text2, oninput: e => text1.val = e.target.value}),
} Here, each input has it´s own state, The source is only updating the target state and vice versa I could not find any differences in performance, but did not really understand, why. So, here are my questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Thanks for your question! First, a note on performance: my hypothesis is that the native DOM implementation is smart enough to skip the execution when the underlying Back to the original questions, for this specific use case, should we use separate states? My opinion is no. First, we want the code reflects the actual application logic, where in this case, the value of 2 input boxes are designed to be synchronized, thus having a single state reflects that exactly. Second, in this case, the application logic is that the values of the input boxes are always in sync with the For the 3rd question, yes it's possible to have an infinite loop due to circular dependency, especially if we code not carefully enough with const a = van.state(0), b = van.state(0)
a.onnew(v => b.val = v + 1)
b.onnew(v => a.val = v + 1)
a.val = 1 |
Beta Was this translation helpful? Give feedback.
-
Great discussion :-) |
Beta Was this translation helpful? Give feedback.
Thanks for your question!
First, a note on performance: my hypothesis is that the native DOM implementation is smart enough to skip the execution when the underlying
value
property is not changed, and the overhead of testing whether the property is changed or not is negligible in benchmark testing. The general, the philosophy of VanJS is not being overly obsessed with performance benchmarking, as there are only sparse scenarios where benchmarking results could translate to noticable difference in the user experience. However, the clarity and simplicity of the code matters in every application.Back to the original questions, for this specific use case, should we use separate states? My op…