27 Aug 2024 |
| @imbev:matrix.org left the room. | 19:18:00 |
30 Aug 2024 |
| @masterphp:matrix.org joined the room. | 17:15:21 |
| @masterphp:matrix.org left the room. | 17:15:51 |
24 Jan 2022 |
| weeb69 set the history visibility to "world_readable". | 16:10:04 |
| weeb69 set the room name to "Support/Help". | 16:10:05 |
| weeb69 invited Discord Bridge. | 16:10:22 |
| Discord Bridge joined the room. | 16:10:24 |
weeb69 | !discord bridge 701068342760570933 703449306497024049 | 16:10:41 |
Discord Bridge | I'm asking permission from the guild administrators to make this bridge. | 16:10:42 |
Discord Bridge | I have bridged this room to your channel
| 16:10:49 |
| Brunkel#2646 joined the room. | 16:59:42 |
Brunkel#2646 | Redacted or Malformed Event | 16:59:42 |
Brunkel#2646 | How can I use the gloo::timers::callback::Interval to increment my timer state each second? I'm confused about how I need to use the closures..
The code:
let timer = use_state(|| 0);
let mut interval: Interval;
fn start_timer() {
interval = Interval::new(1_000, {
let timer = timer.clone();
move || {
timer.set(*timer + 1);
}
});
}
Error msg: use the '|| { ... }' closure form instead | 17:06:40 |
| Voidpumpkin#8396 joined the room. | 17:10:11 |
Voidpumpkin#8396 | is this in astruct component or function component? | 17:10:11 |
Brunkel#2646 | function component | 17:10:22 |
Voidpumpkin#8396 | let timer = use_state(|| 0);
use_effect_with_deps(move |_|{
let interval = Interval::new(1_000, {
let timer = timer.clone();
move || { timer.set(*timer + 1); }
});
},()); | 17:10:28 |
Voidpumpkin#8396 | but be carefull as you timer inside the callback can desync | 17:10:41 |
Voidpumpkin#8396 | yeah actaully nvm this is really bad snippet | 17:10:59 |
Voidpumpkin#8396 | let me fix this | 17:11:05 |
Brunkel#2646 | haha ok | 17:11:14 |
Voidpumpkin#8396 | let timer = use_state(|| 0);
use_effect_with_deps(move |_| {
let timer = timer.clone();
let interval = Interval::new(1_000, move || { timer.set(*timer + 1); });
move || drop(interval)
}, timer); | 17:12:45 |
Voidpumpkin#8396 | we need to recreate the closure every time timer updates | 17:13:08 |
Voidpumpkin#8396 | because other vise *timer will result in an old value | 17:13:23 |
Voidpumpkin#8396 | if dislike recreating timers | 17:13:46 |
Voidpumpkin#8396 | you can use a reducer instead of state | 17:13:53 |
Voidpumpkin#8396 | then yu would just send a some kinf of action like Action::Increment | 17:14:14 |
Voidpumpkin#8396 | and your use_effect_with_deps dependiecies could just be () | 17:14:48 |
Brunkel#2646 | Ah makes sense. I'll use the reducer method. Thanks for the quick response 🙂 | 17:19:14 |
| krytain joined the room. | 20:35:23 |