Get the network speed in react.
npm install @rtbjs/network-speed-testGet the network speed in react.
- network-speed-test from React ToolboxJS
- Installation- @rtbjs/network-speed-test
- Usage
- Issues tracker
To install @rtbjs/network-speed-test, simply use npm or yarn:
``bash`
npm install @rtbjs/network-speed-testor
yarn add @rtbjs/network-speed-test
1. Import useNetworkSpeedTest hook from @rtbjs/network-speed-test.runTest
2. Call returned from the hook.download
3. The result of the test can be obtained from the and upload objects returned from the hook.
`jsx
import { useNetworkSpeedTest } from '@rtbjs/network-speed-test';
import { useEffect } from 'react';
const App = () => {
const { runTest, download, upload } = useNetworkSpeedTest();
useEffect(() => {
runTest();
}, []);
console.log('Download speed is:', download);
console.log('Upload speed is:', upload);
return
export default App;
`
Run the upload test before the download test:
`jsx`
runTest({
runUploadBeforeDownload: true,
});
Run only the download test:
`jsx`
runTest({
upload: false,
});
Run only the upload test:
`jsx`
runTest({
download: false,
});
Types:
`jsx
type UseNetworkSpeedTest = {
runTest: (options?: RunTestOptions) => void; // To run the test
download: {
isRunning: boolean; // If the download test is currently running
isComplete: boolean; // If the test has been completed
result: {
error?: any; // Error
elapsedTime?: number; // How much time the test took
meanClientMbps?: number; // The speed in Mbps
numberOfBytes?: number; // How much data has been sent
};
};
upload: {
isRunning: boolean; // If the upload test is currently running
isComplete: boolean; // If the test has been completed
result: {
error?: any; // Error
elapsedTime?: number; // How much time the test took
meanClientMbps?: number; // The speed in Mbps
numberOfBytes?: number; // How much data has been sent
};
};
isRunning: boolean; // If one of the tests is currently running
isComplete: boolean; // If the both tests have been completed
};
type RunTestOptions = {
download?: boolean; // Set to false if the download test is not needed
upload?: boolean; // Set to false if the upload test is not needed
runUploadBeforeDownload?: boolean; // Set to true if the upload test should be run first
};
``
Please report any issues and feature requests to: issues tracker
---