Skip to content
Discussion options

You must be logged in to vote

Tauri's Events work similar to browser native events, which means that you need to unlisten to them on unMount.

useEffect(() => {
    //listen to a event
    const unlisten = listen("event-name", (e) => {
      console.log(e.payload);
    });

    // invoke a Rust function to start a loop for periodically emitting event.
    start_backend_emitting_loop();

    return () => {
      unlisten.then(f => f());
    }
  }, [] );

Note that start_backend_emitting_loop() needs some test on the backend too, so you don't end up starting that loop multiple times.

P.S. if your component can't realistically unMount (betting on that is really risky btw*), then check abisar's response and disable strict m…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
2 replies
@wangxiaochuTHU
Comment options

@abisar
Comment options

Comment options

You must be logged in to vote
4 replies
@wangxiaochuTHU
Comment options

@wangxiaochuTHU
Comment options

@FabianLars
Comment options

@wgibbs-rs
Comment options

Answer selected by FabianLars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants