You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The component uses _useBluetooth_ hook to detect if Bluetooth API is supported and show all available bluetooth devices and show renders its name after connection.
6
+
*/
7
+
exportconstUseBluetooth=()=>{
8
+
const[value,requestDevice]=useBluetooth();
9
+
10
+
const[error,setError]=useState("");
11
+
12
+
constgetDevice=useCallback(()=>{
13
+
requestDevice().catch(err=>{
14
+
if(errinstanceofError){
15
+
setError(err.message);
16
+
}else{
17
+
setError(errasstring);
18
+
}
19
+
})
20
+
},[requestDevice]);
21
+
22
+
return<>
23
+
<p>
24
+
Bluetooth supported: {value.isSupported ? "Yes" : "No"}
25
+
</p>
26
+
{
27
+
value.isConnected&&
28
+
<p>
29
+
Device Name: {value.device?.name}
30
+
</p>
31
+
}
32
+
<buttontype="button"onClick={getDevice}disabled={!value.isSupported}>Get Bluetooth device</button>
Hook to use [Web Bluetooth API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API).
3
+
4
+
## Usage
5
+
6
+
```tsx
7
+
exportconst UseBluetooth = () => {
8
+
const [value, requestDevice] =useBluetooth();
9
+
10
+
const [error, setError] =useState("");
11
+
12
+
const getDevice =useCallback(() => {
13
+
requestDevice().catch(err=> {
14
+
if (errinstanceofError) {
15
+
setError(err.message);
16
+
} else {
17
+
setError(errasstring);
18
+
}
19
+
})
20
+
}, [requestDevice]);
21
+
22
+
return <>
23
+
<p>
24
+
Bluetooth supported: {value.isSupported?"Yes":"No"}
25
+
</p>
26
+
{
27
+
value.isConnected&&
28
+
<p>
29
+
Device Name: {value.device?.name}
30
+
</p>
31
+
}
32
+
<buttontype="button"onClick={getDevice}disabled={!value.isSupported}>Get Bluetooth device</button>
33
+
{
34
+
error&&
35
+
<pstyle={{color: 'red'}}>
36
+
{error}
37
+
</p>
38
+
}
39
+
</>
40
+
}
41
+
```
42
+
43
+
> The component uses _useBluetooth_ hook to detect if Bluetooth API is supported and show all available bluetooth devices and show renders its name after connection.
> The component has _three internal string states_ and renders three input fields and three components that receive one state each. These three components have an object as internal state with two properties _loading_, initially set to __true__, and _friends_ which is an initially empty array.
Copy file name to clipboardExpand all lines: packages/react-tools/src/hooks/useShare.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,10 @@ import { useCallback } from "react"
5
5
* @returns {{isSupported: boolean, share: (data?: ShareData) => Promise<void>}} object - __isSupported__ to known if share API is supported and __share__ function to use Web share API.
0 commit comments