Dynamic scoping
This commit is contained in:
+18
-1
@@ -23,6 +23,8 @@ fn main() {
|
||||
}
|
||||
|
||||
fn app(cx: &Arc<Context>) -> View {
|
||||
let count = use_state(0);
|
||||
|
||||
{
|
||||
let scope = cx.scope();
|
||||
|
||||
@@ -34,6 +36,13 @@ fn app(cx: &Arc<Context>) -> View {
|
||||
})),
|
||||
);
|
||||
|
||||
cx.on_event({
|
||||
let count = count.clone();
|
||||
move |_, _: &KeyPress| {
|
||||
let mut count = count.get();
|
||||
|
||||
if *count > 5 {
|
||||
if scope.children.lock().unwrap().len() == 1 {
|
||||
scope.child(
|
||||
my_component,
|
||||
Some(Arc::new(|ctx, view| {
|
||||
@@ -43,6 +52,14 @@ fn app(cx: &Arc<Context>) -> View {
|
||||
);
|
||||
}
|
||||
|
||||
*count = 0;
|
||||
}
|
||||
|
||||
*count += 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Arc::new({
|
||||
let cx = cx.clone();
|
||||
|
||||
@@ -53,7 +70,7 @@ fn app(cx: &Arc<Context>) -> View {
|
||||
}
|
||||
|
||||
fn my_component(cx: &Arc<Context>) -> View {
|
||||
let count = use_state(0);
|
||||
let count = use_state(1);
|
||||
|
||||
cx.on_event({
|
||||
let count = count.clone();
|
||||
|
||||
Reference in New Issue
Block a user