Search code examples
javascriptreactjsreact-reduxvideo.jsrangeslider

actions not working inside componentDidMount


I am using react-redux in my application. I have a player and i used rangesliderplugin for videojs. I have an actioncreater which should get triggered when I slide the slider in video timeline. There is a plugin given on https://github.com/danielcebrian/rangeslider-videojs for that. I want to pass a value to my action from this event function. Here is my code

newMarkerTime is my action

class PlayerLogic extends Component {
    constructor() {
        super();
        this.state = {
            player: {}
        };
    }

    componentDidMount() {
        var self = this;
        var options ={hidden:false};
        var player = videojs(this.refs.video, this.props.options).ready(function () {
            self.player = this;
            self.player.on('play', self.handlePlay);
        });
        player.rangeslider(options);
        player.on("sliderchange",function() {
            values = player.getValueSlider();
            this.props.newMarkerTime(values);
        });

This give in an error like this when i slide the slider.

enter image description here


Solution

  • You need to use a fat arrow here, or use self which you declared above

    player.on("sliderchange",function() {
      values = player.getValueSlider();
      // this refers to player
      this.props.newMarkerTime(values);
      // self refers to your component
      self.props.newMarkerTime(values);
    });
    
    // use a fat arrow instead
    player.on("sliderchange", () => {
      values = player.getValueSlider();
      // this refers to your react component
      this.props.newMarkerTime(values);
    });