reactjs - 如何为“react”连接“network-speed”库?
问题描述
给出错误“URL 不是构造函数”
我的代码https://codesandbox.io/s/patient-dust-do1d8?file=/src/App.js
库https://www.npmjs.com/package/network-speed
import React from "react";
const NetworkSpeed = require('network-speed');
const testNetworkSpeed = new NetworkSpeed();
async function getNetworkDownloadSpeed() {
const baseUrl = "http://eu.httpbin.org/stream-bytes/50000000";
const fileSizeInBytes = 50000000;
const speed = await testNetworkSpeed.checkDownloadSpeed(
baseUrl,
fileSizeInBytes
);
console.log(speed);
}
export default function App() {
console.log(NetworkSpeed)
return (
<div className="App">
<button onClick={() => getNetworkDownloadSpeed()}>34</button>
</div>
);
}
解决方案
你不能在 React 中使用那个包。
它不是为了在浏览器中使用,而是在 Node.js 中使用(其中一个是http
和https
内置模块)。
推荐阅读
- python - Django modelcluster:何时/如何使 cached_properties 无效?
- laravel - Laravel 页面没有点击 url
- excel - 参考另一个工作表中的标题
- r - R中所有样本的预测
- java - 警报延迟,警报管理器未在正确时间触发
- ios - InAppPurchase - 仅在新项目中某些产品的标识符无效
- android - 尝试在数据绑定适配器中添加多个属性更改时显示错误
- r - 将表转换为相互依赖/依赖矩阵
- android - 在 Android Studio 中将图像视图居中
- python-3.x - 使用 geopandas 计算多边形中所有线段的长度