@@ -6,6 +6,31 @@ struct Data {
66 pub lists : Vec < Vec < String > > ,
77}
88
9+ pub enum ChannelAction {
10+ NewList ,
11+ AddToList { list : usize , text : String } ,
12+ }
13+
14+ impl DataReducer for Data {
15+ type Action = ChannelAction ;
16+ type Channel = DataChannel ;
17+
18+ fn reduce ( & mut self , message : Self :: Action ) -> Self :: Channel {
19+ match message {
20+ ChannelAction :: NewList => {
21+ self . lists . push ( Vec :: default ( ) ) ;
22+
23+ DataChannel :: ListCreated
24+ }
25+ ChannelAction :: AddToList { list, text } => {
26+ self . lists [ list] . push ( text) ;
27+
28+ DataChannel :: ListN ( list)
29+ }
30+ }
31+ }
32+ }
33+
934#[ derive( PartialEq , Eq , Clone , Debug ) ]
1035pub enum DataChannel {
1136 ListCreated ,
@@ -20,7 +45,7 @@ fn main() {
2045 let mut radio = use_radio :: < Data , DataChannel > ( DataChannel :: ListCreated ) ;
2146
2247 let onclick = move |_| {
23- radio. write ( ) . lists . push ( Vec :: default ( ) ) ;
48+ radio. apply ( ChannelAction :: NewList ) ;
2449 } ;
2550
2651 println ! ( "Running DataChannel::ListCreated" ) ;
@@ -49,7 +74,10 @@ fn ListComp(list_n: usize) -> Element {
4974 rsx ! (
5075 div {
5176 button {
52- onclick: move |_| radio. write( ) . lists[ list_n] . push( "Hello World" . to_string( ) ) ,
77+ onclick: move |_| radio. apply( ChannelAction :: AddToList {
78+ list: 0 ,
79+ text: "Hello, World" . to_string( )
80+ } ) ,
5381 "New Item"
5482 } ,
5583 ul {
0 commit comments