Gathering detailed insights and metrics for @util-hooks/use-tauri-event
Gathering detailed insights and metrics for @util-hooks/use-tauri-event
npm install @util-hooks/use-tauri-event
Typescript
Module System
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
99
Last Day
1
Last Week
3
Last Month
7
Last Year
99
27 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
@util-hooks/use-tauri-event@1.0.0
Unpacked Size
8.56 kB
Size
2.98 kB
File Count
10
Publised On
22 Apr 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
3
Compared to previous week
Last month
250%
7
Compared to previous month
Last year
0%
99
Compared to previous year
3
2
Define custom events and handlers for Tauri events in React.
Tauri is a Rust - JS framework for building custom desktop and mobile apps using a Rust backend with a JS frontend.
Events in Tauri work as they do in the dom, or electron. Rust can emit events from the backend to the frontend, and you can handle them with this custom hook.
1import { useTauriEvent } from "@util-hooks/use-tauri-event"; 2import { invoke } from "@tauri-apps/api"; 3 4const App = () => { 5 const onClick = () => { 6 // Invoke a method that will emit the event from the backend 7 invoke("ping"); 8 }; 9 10 useTauriEvent<string>("pong", msg => { 11 console.log("pong!"); 12 }); 13 14 return <button onClick={onClick}>Click to emit event!</button>; 15};
Or you can pass any state variable to update the event when the state changes, as you would do in a useEffect
hook.
1import { useTauriEvent } from "@util-hooks/use-tauri-event"; 2import { invoke } from "@tauri-apps/api"; 3import { useState } from "react"; 4 5const App = () => { 6 const [count, setCount] = useState<number>(0); 7 8 const onClick = () => { 9 // Invoke a method that will emit the event from the backend 10 invoke("ping"); 11 }; 12 13 useTauriEvent<string>( 14 "pong", 15 msg => { 16 console.log("pong!"); 17 }, 18 [count] 19 ); 20 21 return <button onClick={onClick}>Click to emit event!</button>; 22};
This package provides a builder method to create a custom useTauriEvent
hook, that will follow the types that you pass it whiile building it.
1type CustomEventMap = { 2 ping: { pongMessage: string }; 3 calcSize: number; 4 // ... 5};
buildMappedTauriEventHook
:1import { buildMappedTauriEventHook } from "@util-hooks"; 2 3// You can choose a shorter name :) 4const useCustomTauriHook = buildMappedTauriHook<CustomEventMap>(); 5 6const App = () => { 7 // Here, "ping" and "calcSize" will be inferred, and the payload will have the value from the custom map 8 useCustomTauriHook( 9 "ping", 10 payload => { 11 // This will be valid 12 console.log(payload.pongMessage); // => pong! 13 }, 14 [ 15 /* you can still define state variables */ 16 ] 17 ); 18 19 return <div>Hello, world!</div>; 20};
the tsup and the tauri teams, for making awesome Projects
MIT License © Saverio Scagnoli 2024.
No vulnerabilities found.
No security vulnerabilities found.