question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ref methods not working when using dynamic import with nextjs

See original GitHub issue

Current code=>

import dynamic from "next/dynamic";
const ReactPlayer = dynamic(() => import("react-player/lazy"), { ssr: false });

          <ReactPlayer
              config={config}
              ref={player}
              width="100%"
              height="600px"
              url="//vjs.zencdn.net/v/oceans.mp4"
              muted={true}
              playing={playing}
              controls={true}
              onProgress={}
             />

Current Behavior

Ref methods like player.current.getCurrentTime(); are not working when using dynamic imports with nextjs. Whenever I use methods which are available by assigning ref, I get this error TypeError: player.current.getCurrentTime is not a function

Expected Behavior

expecting ref methods to work.

Steps to Reproduce

  1. Nextjs app with next@12.1.6, react@18.1.0 , react-dom@18.1.0 and react-player@2.10.0 . I’ve already tested with react-player@2.10.1. Not working in that version as well.

Environment

  • Browser: Chrome

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:6
  • Comments:6

github_iconTop GitHub Comments

7reactions
inderrrcommented, Aug 6, 2022

The only solution so far is to wrap the player in another component and import that component. Then we can use dynamic imports and the ref methods work as expected.

// components/VideoPlayer.js
import ReactPlayer from "react-player/lazy";

function VideoPlayer({ playerRef }) {
  return (
    <ReactPlayer
        ref={playerRef}
        muted
        playing
        controls        
      />
  );
}

// pages/index.js
import dynamic from "next/dynamic";
const VideoPlayer = dynamic(() => import("../components/VideoPlayer"), {ssr: false});

function Home(){
 const playerRef=useRef();
 
 return <VideoPlayer playerRef={playerRef} />

}
3reactions
LuLue7775commented, May 31, 2022

Same issue here. I’m using useRef to access ReactPlayer, and somehow it’s returing retry() instead of any other instance methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ref doesn't work with dynamically imported components #4957
Import a component using next/dynamic; Use said component in render passing a ref (use a function to received the ref).
Read more >
`forwardRef` error when dynamically importing a module in ...
import dynamic from "next/dynamic"; import { useRef, useState, forwardRef } from "react"; const Editor = dynamic(() => import(".
Read more >
Advanced Features: Dynamic Import - Next.js
Dynamically import JavaScript modules and React Components and split your code into manageable chunks.
Read more >
Dynamic imports in Next.JS - Medium
Dynamic imports in Next.JS · Dynamic imports are fetched when the component is rendered the first time. · First, import the dynamic function...
Read more >
TypeScript error when using next/dynamic with ref - Reddit
So, when not using next/dynamic all is good. ... next.js github issues: https://github.com/vercel/next.js/issues/4957#issuecomment-413841689.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found