Skip to content

Commit

Permalink
Create counter_redux_nested.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Jun 9, 2023
1 parent 16f9660 commit f08684c
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/counter_redux_nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use rui::*;

// This example shows how to mix the redux style, with state local
// to a view.

#[derive(Clone)]
enum Action {
Increment,
None,
}

struct AppState {
count: i32,
}

impl AppState {
fn new() -> Self {
AppState { count: 1 }
}
}

fn reduce(state: &mut AppState, action: &Action) {
match action {
Action::Increment => state.count += 1,
Action::None => (),
}
}

fn main() {
rui(redux(AppState::new, reduce, |app_state| {
vstack((
format!("{}", app_state.count).padding(Auto),
state(|| 0, |handle, _| {
button("increment every 5 clicks", move|cx| {
cx[handle] += 1;
if cx[handle] == 5 {
cx[handle] = 0;
Action::Increment
} else {
Action::None
}
})
}),
))
}));
}

0 comments on commit f08684c

Please sign in to comment.