-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ref methods not working when using dynamic import with nextjs #1455
Comments
Same issue here. I'm using useRef to access ReactPlayer, and somehow it's returing retry() instead of any other instance methods. |
Can anybody share a codesandbox Or stackblitz link would be helpful to work ok this issue. |
This issue is related to dynamic import behaviour instead of react-player package. import {LegacyRef} from 'react'
import ReactPlayer, {ReactPlayerProps} from 'react-player/lazy'
export default function ReactPlayerWrapper(
props: ReactPlayerProps & {
playerRef: LegacyRef<ReactPlayer>
},
) {
return <ReactPlayer ref={props.playerRef} {...props} />
} |
why not use forwardRef here? |
We're having the same issue at our end as well. Opting out of lazy loading is not an option due to the size of the library, and the solutions above doesn't seem to address it correctly. I'll be happy to test any other proposals, if there are any. |
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} />
} |
UPDATE: sorry, i should post it in next not here.
some plant to fix it? i tried it but still only
the output is
|
this is due to the fact that the word ref is reserved by the react itself and apparently the developers didn’t think about this moment, so with a dynamic improte for the ref you need to create a wrapper with the forwardedRef prop and in turn pass it as a normal prop
|
|
Current code=>
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 errorTypeError: player.current.getCurrentTime is not a function
Expected Behavior
expecting ref methods to work.
Steps to Reproduce
[email protected]
,[email protected]
,[email protected]
and[email protected]
. I've already tested with[email protected]
. Not working in that version as well.Environment
The text was updated successfully, but these errors were encountered: