reactjs - 为什么我会得到 ReferenceError : RTCPeerConnection is not defined in Next. js?

标签 reactjs next.js webapi

我在尝试 const pc = new RTCPeerConnection() 时收到 ReferenceError: RTCPeerConnection is not defined。我该如何克服这个错误?

这不是我的浏览器,我可以在它们上本地运行 webRTC

最佳答案

Next.js pre-renders every page在服务器上。在页面被预呈现时尝试使用 Web API 将引发与您看到的错误类似的错误,因为这些 Web API 不存在于 Node.js 环境中。

要解决它,请确保在组件的 useEffect 中调用 new RTCPeerConnection(),这样它只会在客户端调用。

useEffect(() => {
    const pc = new RTCPeerConnection()
    // Rest of your logic here
}, [])

关于reactjs - 为什么我会得到 ReferenceError : RTCPeerConnection is not defined in Next. js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68838976/

相关文章:

reactjs - React-Admin:如何访问没有 `source` 的值?

android - Expo Developer Tools 与 Expo CLI 断开连接

next.js - 在 getStaticProps 中使用路由器

php - 如何通过 TinyURL api 缩短包含 "#"符号的 url?

reactjs - "TypeError: Object(...) is not a function" react -redux-firebase

javascript - React 中数组的操作

javascript - Cloud Firestore 可以与 .get() 一起正常工作,但不能与使用 next.js 的 .onSnapshot() 一起工作

javascript - 使用 React js 的实时加密表

wpf - SignalR Core 适用于 localhost 但不适用于 Azure 应用服务