You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+56Lines changed: 56 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -993,10 +993,66 @@ In short, selective reaction states that only the views that depend on a state w
993
993
##### Anonymous Components
994
994
Sometimes a state change should not cause a component to rerender in the application. For example, consider a Count state. When the Count state changes, you only want to rerender the number, not the whole component. In that case, use anonymous components.
995
995
996
+
In OpenScript.Js, the `v` function, representing `valueOf` is used to create an anonymous component. The `v` indicates that you only want to use the value of a state. It passes the value of the state to a provided callback function which should return an `OSM` or a `string`. By default, it returns the value of the state.
997
+
996
998
*Syntax*
997
999
```js
998
1000
v(state, callback(value) =>OSM| string);
1001
+
```
1002
+
1003
+
*Usage*
1004
+
```js
1005
+
let counter =state(0);
1006
+
1007
+
functionClock(...args) {
1008
+
returnh.div(
1009
+
{class:'d-flex mb-3'},
1010
+
h.span('I will not rerender when counter change'),
1011
+
1012
+
v(counter), // counter.value --> will change when counter value changes
1013
+
1014
+
...args
1015
+
);
1016
+
}
1017
+
1018
+
h.Clock({
1019
+
parent: root,
1020
+
$class:'d-block',
1021
+
});
1022
+
1023
+
// change counter value
1024
+
setTimeout(() =>counter.value++, 1000);
1025
+
```
1026
+
You can pass a callback function to the `v` function and return a formatted string. For example, returning the time in hours:minutes:seconds.
1027
+
1028
+
```js
1029
+
functionClock(...args) {
1030
+
returnh.div(
1031
+
{class:'d-flex mb-3'},
1032
+
h.span('I will not rerender when counter change'),
0 commit comments