@@ -10,22 +10,38 @@ use wasm_bindgen::prelude::*;
1010#[ global_allocator]
1111static ALLOC : wee_alloc:: WeeAlloc = wee_alloc:: WeeAlloc :: INIT ;
1212
13- // Counter component with reactive state
14- struct CounterApp {
15- count : State < i32 > ,
16- }
17-
18- impl CounterApp {
19- fn new ( ) -> Self {
20- Self {
21- count : State :: new ( 0 ) ,
22- }
23- }
24- }
13+ // Counter component using hooks
14+ struct CounterApp ;
2515
2616impl Component for CounterApp {
2717 fn render ( & self ) -> Element {
28- let count_value = self . count . get ( ) ;
18+ // Use the hooks system for state management
19+ let ( count, set_count) = use_state_hook ( 0 ) ;
20+
21+ // Use effect to log count changes
22+ use_effect ( count, {
23+ let count = count;
24+ move || {
25+ web_sys:: console:: log_1 ( & format ! ( "Count changed to: {}" , count) . into ( ) ) ;
26+ || { } // Cleanup function
27+ }
28+ } ) ;
29+
30+ // Create increment and decrement callbacks
31+ let increment = {
32+ let set_count = set_count. clone ( ) ;
33+ move || set_count ( count + 1 )
34+ } ;
35+
36+ let decrement = {
37+ let set_count = set_count. clone ( ) ;
38+ move || set_count ( count - 1 )
39+ } ;
40+
41+ let reset = {
42+ let set_count = set_count. clone ( ) ;
43+ move || set_count ( 0 )
44+ } ;
2945
3046 // Create the root element with reactive event handlers
3147 Element :: Node {
@@ -60,7 +76,7 @@ impl Component for CounterApp {
6076 class: Some ( "counter-value" . to_string( ) ) ,
6177 ..Default :: default ( )
6278 } ,
63- children: vec![ Element :: Text ( format!( "Count: {}" , count_value ) ) ] ,
79+ children: vec![ Element :: Text ( format!( "Count: {}" , count ) ) ] ,
6480 } ,
6581 // Button container
6682 Element :: Node {
@@ -75,10 +91,7 @@ impl Component for CounterApp {
7591 tag: "button" . to_string( ) ,
7692 props: Props {
7793 class: Some ( "btn btn-primary" . to_string( ) ) ,
78- on_click: Some ( std:: rc:: Rc :: new( {
79- let count = self . count. clone( ) ;
80- move || count. set( count. get( ) + 1 )
81- } ) ) ,
94+ on_click: Some ( std:: rc:: Rc :: new( increment. clone( ) ) ) ,
8295 ..Default :: default ( )
8396 } ,
8497 children: vec![ Element :: Text ( "Increment" . to_string( ) ) ] ,
@@ -88,10 +101,7 @@ impl Component for CounterApp {
88101 tag: "button" . to_string( ) ,
89102 props: Props {
90103 class: Some ( "btn btn-secondary" . to_string( ) ) ,
91- on_click: Some ( std:: rc:: Rc :: new( {
92- let count = self . count. clone( ) ;
93- move || count. set( count. get( ) - 1 )
94- } ) ) ,
104+ on_click: Some ( std:: rc:: Rc :: new( decrement. clone( ) ) ) ,
95105 ..Default :: default ( )
96106 } ,
97107 children: vec![ Element :: Text ( "Decrement" . to_string( ) ) ] ,
@@ -101,10 +111,7 @@ impl Component for CounterApp {
101111 tag: "button" . to_string( ) ,
102112 props: Props {
103113 class: Some ( "btn btn-warning" . to_string( ) ) ,
104- on_click: Some ( std:: rc:: Rc :: new( {
105- let count = self . count. clone( ) ;
106- move || count. set( 0 )
107- } ) ) ,
114+ on_click: Some ( std:: rc:: Rc :: new( reset) ) ,
108115 ..Default :: default ( )
109116 } ,
110117 children: vec![ Element :: Text ( "Reset" . to_string( ) ) ] ,
@@ -139,8 +146,7 @@ pub fn main() -> Result<(), JsValue> {
139146 console_error_panic_hook:: set_once ( ) ;
140147
141148 // Create and mount the app using the reactive renderer
142- let app = CounterApp :: new ( ) ;
143- mount ( Box :: new ( app) , "root" ) ;
149+ mount ( Box :: new ( CounterApp ) , "root" ) ;
144150
145151 // Log success
146152 web_sys:: console:: log_1 ( & "Layer9 Counter App initialized with reactive rendering!" . into ( ) ) ;
0 commit comments