Skip to content

Commit

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

// This example shows how to nest a redux inside another redux.

#[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 => (),
}
}

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

struct LocalState {
count: i32,
}

fn reduce_local(state: &mut LocalState, action: &LocalAction) -> Action {
match action {
LocalAction::Increment => {
state.count += 1;
if state.count == 5 {
state.count = 0;
Action::Increment
} else {
Action::None
}
}
}
}

fn main() {
rui(redux(AppState::new, reduce, |app_state| {
vstack((
format!("{}", app_state.count).padding(Auto),
redux(|| LocalState{ count: 0 }, reduce_local, |_| {
button_a("increment every 5 clicks", LocalAction::Increment)
}),
))
}));
}

0 comments on commit 7e94921

Please sign in to comment.