Giter VIP home page Giter VIP logo

Comments (2)

dmotz avatar dmotz commented on May 12, 2024 2

The problem is that conditional that checks for window makes TypeScript think it could be undefined and not able to be destructured. Is that client check necessary if you're using 'use client'?

In any case, makeAction() isn't really a hook, just a normal function, so you can call it conditionally wherever you'd like.

from trystero.

tony1658 avatar tony1658 commented on May 12, 2024

Next.JS 13/14 renders everything completely on the server by default.

use client does opt out of React Server Components but you can still get Server Side Rendering and client-side hydration with Next.JS.

So if I use the following code:

"use client"
import {joinRoom} from 'trystero'
import {useEffect, useRef} from 'react'

export const useRoom = (roomConfig, roomId) => {
  const roomRef = useRef(joinRoom(roomConfig, roomId))

  useEffect(() => {
    roomRef.current = joinRoom(roomConfig, roomId)
    return () => roomRef.current.leave()
  }, [roomConfig, roomId])

  return roomRef.current
}

I'll get the following error from trystero:

 node_modules/simple-peer-light/index.js (98:0) @ new Peer
 ⨯ unhandledRejection: Error: No WebRTC support: Specify `opts.wrtc` option in this environment
    at new Peer (webpack-internal:///(ssr)/./node_modules/simple-peer-light/index.js:102:11)
    at initPeer (webpack-internal:///(ssr)/./node_modules/trystero/src/utils.js:26:16)
    at eval (webpack-internal:///(ssr)/./node_modules/trystero/src/torrent.js:64:75)
    at Array.map (<anonymous>)
    at makeOffers (webpack-internal:///(ssr)/./node_modules/trystero/src/torrent.js:63:10)
    at makeOfferPool (webpack-internal:///(ssr)/./node_modules/trystero/src/torrent.js:73:31)
    at announceAll (webpack-internal:///(ssr)/./node_modules/trystero/src/torrent.js:235:17) {
  code: 'ERR_WEBRTC_SUPPORT'

So directly calling joinRoom in the useRef doesn't work with Next.JS. I could make a new issue if you consider this a bug.

Anyway, being able to call makeAction conditionally solves this problem. Thanks for the tip!

from trystero.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.