ref methods not working when using dynamic import with nextjs
See original GitHub issueCurrent 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
- Nextjs app with
next@12.1.6
,react@18.1.0
,react-dom@18.1.0
andreact-player@2.10.0
. I’ve already tested withreact-player@2.10.1
. Not working in that version as well.
Environment
- Browser: Chrome
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Same issue here. I’m using useRef to access ReactPlayer, and somehow it’s returing retry() instead of any other instance methods.