Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to spawn a thread in main or other place only once #2391

Closed
jiker-burce opened this issue May 5, 2024 · 2 comments
Closed

how to spawn a thread in main or other place only once #2391

jiker-burce opened this issue May 5, 2024 · 2 comments

Comments

@jiker-burce
Copy link

Problem

Steps To Reproduce

Steps to reproduce the behavior:

fn main() {
launch(client_mod::client_lib::Client);
}

pub fn Client() -> Element {
    println!("Client");

  spawn(async move {
  // do some async things here
  }
}
  • I found that if something changed, the Client(main App component) will be rendered again, so the Client will be printed many times. So that the spawn will generate many times.

Expected behavior
Is there a way could spawn new thread in fn main or other places, so I could just spawn a new thread once just like launch().

Screenshots

If applicable, add screenshots to help explain your problem.

Environment:

  • Dioxus version: 0.5.1
  • Rust version: 1.7.75
  • OS info: MacOS
  • App platform: desktop
@jkelleyrtp
Copy link
Member

Problem

Steps To Reproduce

Steps to reproduce the behavior:

fn main() {
launch(client_mod::client_lib::Client);
}

pub fn Client() -> Element {
    println!("Client");

  spawn(async move {
  // do some async things here
  }
}
  • I found that if something changed, the Client(main App component) will be rendered again, so the Client will be printed many times. So that the spawn will generate many times.

Expected behavior Is there a way could spawn new thread in fn main or other places, so I could just spawn a new thread once just like launch().

Screenshots

If applicable, add screenshots to help explain your problem.

Environment:

  • Dioxus version: 0.5.1
  • Rust version: 1.7.75
  • OS info: MacOS
  • App platform: desktop

We have use_future, use_effect, and use_hook depending on how much abstraction you need.

https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_future.html
https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_effect.html
https://docs.rs/dioxus-core/0.5.1/dioxus_core/fn.use_hook.html

@jiker-burce
Copy link
Author

jiker-burce commented May 6, 2024

@jkelleyrtp thanks for the reply. but I found that to use the spawn() in the top element fn() could solve the problem. We should move the values that would be rerendered in the child component.

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

No branches or pull requests

2 participants