Skip to content
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

Closed
singh-inder opened this issue May 15, 2022 · 10 comments
Closed

ref methods not working when using dynamic import with nextjs #1455

singh-inder opened this issue May 15, 2022 · 10 comments

Comments

@singh-inder
Copy link

singh-inder commented May 15, 2022

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 [email protected], [email protected] , [email protected] and [email protected] . I've already tested with [email protected]. Not working in that version as well.

Environment

  • Browser: Chrome
@LuLue7775
Copy link

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

@b4s36t4
Copy link

b4s36t4 commented Jun 2, 2022

Can anybody share a codesandbox Or stackblitz link would be helpful to work ok this issue.

@minhtc
Copy link

minhtc commented Jun 2, 2022

This issue is related to dynamic import behaviour instead of react-player package.
You can simply wrap the ReactPlayer in a custom component then use a custom props to pass the player ref to parent.
Example:
ReactPlayerWrapper.tsx

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} />
}

@davidkhierl
Copy link

why not use forwardRef here?

@Multiply
Copy link

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.

@singh-inder
Copy link
Author

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} />

}

@geminiyellow
Copy link

geminiyellow commented Apr 27, 2023

UPDATE: sorry, i should post it in next not here.

The only solution so far is to wrap the player in another component and import that component.

some plant to fix it?

i tried it but still only retry in current

// components/Chart.ts

import React from 'react'
export const Chart = ({ ref }: any) => <div ref={ref}>Chart</div>
Chart.displayName = 'Chart'
export default Chart


// TrendCharts.ts
const Chart = dynamic(() => import('@material/Chart'), { ssr: false })

export const TrendCharts: FC<any> = (props) => {
  const $ref = useRef(null)
  useEffect(() => { console.log($ref) }, [$ref])
  return (<Chart ref={$ref} ... />)
}

the output is

{current: {…}}
  current: 
    retry: ƒ ()

@vasabigreencolor
Copy link

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

import React, { RefObject } from 'react'
import AppModal, { IAppModalProps, IAppModalRef } from '.'

export interface IDynamicAppModalProps extends IAppModalProps {
  forwardedRef?: RefObject<IAppModalRef>
}

const DynamicAppModal: React.FC<IDynamicAppModalProps> = (props) => {
  const { forwardedRef, ...args } = props
  return <AppModal ref={forwardedRef} {...args} />
}

export default DynamicAppModal

@rendi12345678
Copy link

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} />

}

screenshot-20240828-080842Z-all
it works thanks
screenshot-20240828-080835Z-all

@rendi12345678
Copy link

screenshot-20240828-080825Z-all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants