Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Add an example with mutable state #300

Open
David-OConnor opened this issue Mar 25, 2020 · 1 comment
Open

Add an example with mutable state #300

David-OConnor opened this issue Mar 25, 2020 · 1 comment

Comments

@David-OConnor
Copy link

I'm not sure how to go about adding mutable shared state. I feel like this is a very common thing to want in a UI app, but I can't find it in the examples. I'm suspicious the solution involves Rc<RefCell<T>>, but am not sure how to do it. Eg:

let state = Rc::new(RefCell::new(state));  // Interior mutability to solve this?

let btn1 = Button::new_with_label("1");
btn1.connect_clicked(|_| {
    state.borrow_mut().value1 = true;
 });

let btn2 = Button::new_with_label("2");
btn2.connect_clicked(|_| {
    state.borrow_mut().value2 = true;
});
@piegamesde
Copy link

That's pretty much how you do it, yes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants