use-network

Get current connection status
Import

Usage

use-network hook returns an object with current connection status:

PropertyValue
Online
Online
rtt50
downlink10
effectiveType4g
saveData
false
import { Text, Table } from '@mantine/core';
import { useNetwork } from '@mantine/hooks';
function Demo() {
const networkStatus = useNetwork();
return (
<Table sx={{ maxWidth: 300, tableLayout: 'fixed' }} mx="auto">
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Online</td>
<td>
<Text size="sm" color={networkStatus.online ? 'teal' : 'red'}>
{networkStatus.online ? 'Online' : 'Offline'}
</Text>
</td>
</tr>
<tr>
<td>rtt</td>
<td>{networkStatus.rtt}</td>
</tr>
<tr>
<td>downlink</td>
<td>{networkStatus.downlink}</td>
</tr>
<tr>
<td>effectiveType</td>
<td>{networkStatus.effectiveType}</td>
</tr>
<tr>
<td>saveData</td>
<td>
<Text size="sm" color={networkStatus.saveData ? 'teal' : 'red'}>
{networkStatus.saveData ? 'true' : 'false'}
</Text>
</td>
</tr>
</tbody>
</Table>
);
}

Browser support

use-network uses experimental navigator.connection, see browser compatibility table.

Definition

function useNetwork(): {
downlink: number;
effectiveType: 'slow-2g' | '2g' | '3g' | '4g';
saveData: boolean;
rtt: number;
online: boolean;
};