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

How to use useRef in functional components? #861

Closed
josoroma-zz opened this issue Apr 27, 2020 · 2 comments
Closed

How to use useRef in functional components? #861

josoroma-zz opened this issue Apr 27, 2020 · 2 comments

Comments

@josoroma-zz
Copy link

josoroma-zz commented Apr 27, 2020

Hi!

I am probably missing something... I guess. But I am not able to use seekTo in the handleSeekMouseUp method:

nlKHXCB6FZ

import React, { useRef, useState } from 'react';
import ReactPlayer from 'react-player'
import Button from '@material-ui/core/Button';
import Container from '@material-ui/core/Container';

import useStyles from './index.style.js';

const Player = () => {
  const classes = useStyles();

  const inputRange = useRef(null);

  const [playing, setPlaying] = useState(true);
  const [seeking, setSeeking] = useState(false);

  const [loaded, setLoaded] = useState(0);
  const [duration, setDuration] = useState(0);
  const [played, setPlayed] = useState(0);

  console.log('---> playing', playing);
  console.log('---> seeking', seeking);
  console.log('---> played', played);
  console.log('---> duration', duration);

  const handlePlayPause = () => {
    setPlaying(!playing);
  }

  const handleSeekMouseDown = e => {
    setSeeking(true);
  }

  const handleSeekChange = e => {
    setPlayed(parseFloat(e.target.value));
  }

  const handleSeekMouseUp = e => {
    console.log('***** e.target.value', e.target.value);
    setSeeking(false);
    inputRange.current.seekTo(parseFloat(e.target.value));
  }

  const handleProgress = state => {
    console.log('onProgress', state);
    if (!seeking) {
      setPlayed(state.played);
    }
  }

  const handleDuration = (duration) => {
    console.log('onDuration', duration);
    setDuration(duration);
  }

  return (
    <div className={classes.root}>
      <ReactPlayer
        ref={inputRange}
        className={classes.reactPlayer}
        playing={playing}
        url='https://storage.googleapis.com/media-session/elephants-dream/the-wires.mp3'
        controls={false}
        volume={1}
        loop={true}
        onProgress={handleProgress}
        onDuration={handleDuration}
      />
      <Container maxWidth="sm">
         &nbsp; Seek: &nbsp;
        <input
          type='range' min={0} max={0.999999} step='any'
          value={played}
          onMouseDown={handleSeekMouseDown}
          onChange={handleSeekChange}
          onMouseUp={handleSeekMouseUp}
        />
        &nbsp; Played: &nbsp;
        <progress max={1} value={played} />
         &nbsp; Control: &nbsp;
        <button onClick={handlePlayPause}>{playing ? 'Pause' : 'Play'}</button>
      </Container>
    </div>
  );
};

export default Player;

Thanks!

@cookpete
Copy link
Owner

Fixed in v2.0.1

@josoroma-zz
Copy link
Author

Thanks a bunch @cookpete

albanqoku added a commit to albanqoku/react-player that referenced this issue Feb 24, 2021
Webmaster1116 added a commit to Webmaster1116/video-player that referenced this issue May 20, 2021
webmiraclepro added a commit to webmiraclepro/video-player that referenced this issue Sep 9, 2022
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

2 participants