@@ -9,83 +9,93 @@ and btw: It is currently not meant to ever be used in production.
99
1010## Examples
1111
12- ### Stateless component (not a real API difference to stateful components - just a pattern)
12+ ### Example 1
1313
1414``` jsx
15- import { component , h , prop , render } from ' js-elements'
16-
17- const SayHello = component (' say-hello' , {
18- props: {
19- salutation: prop .str .opt (' Hello' ),
20- name: prop .str .opt (' World' )
15+ import { element , h , prop , render } from ' js-elements'
16+
17+ @element (' say-hello' )
18+ class SayHello {
19+ @prop ()
20+ salutation = ' Hello'
21+
22+ @prop ()
23+ name = ' World'
24+
25+ static main (self ) {
26+ return () => (
27+ < div>
28+ {self .salutation }, {self .name }!
29+ < / div>
30+ )
2131 }
22- })((c , props ) => {
23- return () => (
24- < div>
25- {props .salutation }, {props .name }!
26- < / div>
27- )
28- })
32+ }
2933
3034render (< SayHello salutation= " Hi" name= " Jane Doe" / > , ' #app' )
3135```
3236
33- ### Stateful component
37+ ### Example 2
3438
3539``` jsx
36- import { component , h , prop , render } from ' js-elements'
37- import { useEffect , useState , useStyles } from ' js-elements/hooks'
40+ import { element , h , prop , render } from ' js-elements'
41+ import { useEffect , useOnMount , useState , useStyles } from ' js-elements/hooks'
3842import counterStyles from ' ./counter.css'
3943
40- const Counter = component (' demo-counter' , {
41- props: {
42- initialCount: prop .num .opt (0 ),
43- label: prop .str .opt (' Counter' )
44+ @element (' demo-counter' )
45+ class Counter {
46+ @prop ()
47+ initialCount = 0
48+
49+ @prop ()
50+ label = ' Counter'
51+
52+ static main (self ) {
53+ const [state , setState ] = useState ({
54+ count: props .initialCount
55+ })
56+
57+ const onClick = () => setState (' count' , (it ) => it + 1 )
58+
59+ useStyles (counterStyles)
60+
61+ useOnMount (() => {
62+ console .log (` "${ self .label } " has been mounted` )
63+
64+ return () => console .log (` Unmounting "${ self .label } "` )
65+ })
66+
67+ useEffect (
68+ () => console .log (` Value of "${ self .label } ": ${ state .count } ` ),
69+ () => [state .count ]
70+ )
71+
72+ return () => (
73+ < div class = " counter" >
74+ < label class = " label" > {self .label }: < / label>
75+ < button class = " button" onClick= {onClick}>
76+ {state .count }
77+ < / button>
78+ < / div>
79+ )
4480 }
45- })((c , props ) => {
46- const [state , setState ] = useState (c, {
47- count: props .initialCount
48- })
49-
50- const onClick = () => setState (' count' , it => it + 1 )
51-
52- useStyles (c, counterStyles)
53-
54- useOnMount (c, () => {
55- console .log (` "${ props .label } " has been mounted` )
56-
57- return () => console .log (` Unmounting "${ props .label } "` )
58- })
59-
60- useEffect (c)(
61- () => console .log (` Value of "${ props .label } ": ${ count} ` ),
62- () => [count]
63- )
64-
65- return () => (
66- < div class = " counter" >
67- < label class = " label" > {props .label }: < / label>
68- < button class = " button" onClick= {onClick}>
69- {count}
70- < / button>
71- < / div>
72- )
73- })
81+ }
7482
7583render (< Counter / > , ' #app' )
7684```
7785
78- _ js-elements_ supports so-called "hooks" which are
79- functions similar to React hooks (but without all the magic).
80- All of those "hooks" can be written completely in userland
81- (means there are no such things like "core hooks").
86+ ### Example 3
8287
8388``` jsx
84- import { component , h } from ' js-elements'
89+ import { element , h , render } from ' js-elements'
8590import { useTime } from ' js-elements/hooks'
8691
87- const Clock = component (' demo-clock' , (c ) => {
88- const getTime = useTime (c, 1000 , () => new Date ().toLocaleTimeString ())
89- return () => < div> Current time: {getTime ()}< / div>
90- })
92+ @element (' demo-clock' )
93+ class DemoClock {
94+ static main () {
95+ const getTime = useTime (1000 , () => new Date ().toLocaleTimeString ())
96+ return () => < div> Current time: {getTime ()}< / div>
97+ }
98+ }
99+
100+ render (< DemoClock / > , ' #app' )
91101```
0 commit comments