@@ -14,12 +14,12 @@ and btw: It is currently not meant to ever be used in production.
1414``` jsx
1515import { component , h , prop , render } from ' js-elements'
1616
17- const SayHello = component (' say-hello' , {
17+ const SayHello = component (' say-hello' )( {
1818 props: {
1919 salutation: prop .str .opt (' Hello' ),
2020 name: prop .str .opt (' World' )
2121 }
22- }, (c , props ) => {
22+ })( (c , props ) => {
2323 return () => (
2424 < div>
2525 {props .salutation }, {props .name }!
@@ -34,26 +34,30 @@ render(<SayHello salutation="Hi" name="Jane Doe" />, '#app')
3434
3535``` jsx
3636import { component , h , prop , render } from ' js-elements'
37+ import { useEffect , useState , useStyles } from ' js-elements/hooks'
3738import counterStyles from ' ./counter.css'
3839
39- const Counter = component (' demo-counter' , {
40+ const Counter = component (' demo-counter' )( {
4041 props: {
4142 initialCount: prop .num .opt (0 ),
4243 label: prop .str .opt (' Counter' )
4344 }
44- }, (c , props ) => {
45- let count = props .initialCount
46-
47- const onClick = () => {
48- ++ count
49- c .refresh ()
50- }
51-
52- c .addStyles (counterStyles)
53- c .afterMount (() => console .log (` "${ props .label } " has been mounted` ))
54- c .beforeUnmount (() => console .log (` Unmounting "${ props .label } "` ))
55-
56- c .effect (
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)(
5761 () => console .log (` Value of "${ props .label } ": ${ count} ` ),
5862 () => [count]
5963 )
@@ -71,47 +75,16 @@ const Counter = component('demo-counter', {
7175render (< Counter / > , ' #app' )
7276```
7377
74- Unfortunatlely, if you are using the prettier code formatter, the
75- above shown syntax will be reformatted in a way that you may not
76- necessarily want.
77- Therefore the following more prettier friendly alternative syntax
78- is also allowed:
79-
80- ``` jsx
81- import { component , h , prop } from ' js-elements'
82-
83- export default component (' demo-counter' , {
84- props: {
85- initialCount: prop .num .opt (0 ),
86- label: prop .str .opt (' Counter' )
87- }
88- }).from ((c , props ) => {
89- let count = props .initialCount
90-
91- const onClick = () => {
92- ++ count
93- c .refresh ()
94- }
95-
96- return () => (
97- < div>
98- < label> {props .label }: < / label>
99- < button onClick= {onClick}> {count}< / button>
100- < / div>
101- )
102- })
103- ```
104-
105- _ js-elements_ also supports so-called "extensions" which are
78+ _ js-elements_ supports so-called "hooks" which are
10679functions similar to React hooks (but without all the magic).
107- All of those "extensions " can be written completely in userland
108- (means there are no such things like "core extensions ").
80+ All of those "hooks " can be written completely in userland
81+ (means there are no such things like "core hooks ").
10982
11083``` jsx
11184import { component , h } from ' js-elements'
112- import { useTime } from ' js-elements/ext '
85+ import { useTime } from ' js-elements/hooks '
11386
114- const Clock = component (' demo-clock' , (c ) => {
87+ const Clock = component (' demo-clock' )( (c ) => {
11588 const getTime = useTime (c, 1000 , () => new Date ().toLocaleTimeString ())
11689 return () => < div> Current time: {getTime ()}< / div>
11790})
0 commit comments