从另一个组件调用React VideoJS组件的方法

时间:2016-10-20 11:35:41

标签: javascript reactjs react-redux

我的反应项目中有一个带有标记的VideoJs组件。我有一个名为jumpToSpecificMarker的函数。我有另一个组件hasjumpToSpecificMarker,它有这些标记的名称。我想要做的就是每当我点击这个listview的listItem时调用jumpToSpecificMarker。目前我正在将onTouchTap事件中的prop传递给listItem。我想传递它以及在单击listItem时触发jumpToSpecificMarker函数。每当我点击listItem时,我都在使用redux来处理状态。

这是我的代码

export default class PlayerLogic extends Component{
    constructor() {
        super();
        this.state = {
            player : {}
        };
    }
    componentDidMount() {
        var self = this;
        var player = videojs(this.refs.video, this.props.options).ready(function () {
            self.player = this;
            self.player.on('play', self.handlePlay);
        });

        if (this.props.onPlayerInit) this.props.onPlayerInit(player);
         player.markers({
            markerStyle: {},
            markers: [
                {length: 8, startTime: 10, endTime: 15, time: 9.5, text: "Cigarette"},
                {length: 2, startTime: 20, endTime: 25, time: 16, text: "Cigarette"},
                {length: 15, startTime: 30, endTime: 38, time: 23.6, text: "Cigarette"},

            ]
        });

        this.setState({ player: player});
    }

    jumpToSpecificMarker(i) {
        this.state.player.markers.jumpToSpecificMarker(i);
    }

    render() {
        var props = blacklist(this.props, 'children', 'className', 'src', 'type', 'onPlay');
        props.className = cx(this.props.className, 'videojs', 'video-js vjs-default-skin', 'vjs-big-play-centered');


        assign(props, {
            ref: 'video',
            controls: true,
            width: "700", height: "450"
        });


        return (
            <div>
                <video {... props}>
                    <source src={this.props.src} type={this.props.type} id={this.props.id}/>
                </video>
            </div>)

    }
}
const mapStateToProps =(state) =>{
    return {
        tags: state.tagReducer
    };
};

export default connect(mapStateToProps)(PlayerLogic);

我想从另一个组件中调用上面组件中的jumpToSpecificMarker函数

class TagListItemDetails extends Component {
    handleButtonClick() {
        browserHistory.push("TagList")
    }

    render() {

        return (
            <MuiThemeProvider>
                <div>
                    <List id="parent-list-tags">
                        <ListItem primaryText="Kitchen" onTouchTap={() => this.props.tagSelected(0)}/>
                        <ListItem primaryText="Beach" onTouchTap={() => this.props.tagSelected(1)}/>
                        <ListItem primaryText="Marriage" onTouchTap={() => this.props.tagSelected(2)}/>
                        <ListItem primaryText="Garden" onTouchTap={() => this.props.tagSelected(3)}/>
                    </List>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                </div>
            </MuiThemeProvider>

        );
    }
}

const mapStateToProps =(state) =>{
    return {
        tags: state.tagReducer
    };
};

function matchDispatchToProps(dispatch){
    return bindActionCreators({tagSelected: tagSelected}, dispatch);
}

export default connect(mapStateToProps, matchDispatchToProps)(TagListItemDetails);

1 个答案:

答案 0 :(得分:0)

您在哪里使用在此代码块中定义的self.player属性?

componentDidMount() {
    var self = this;
    var player = videojs(this.refs.video, this.props.options).ready(function () {
       self.player = this;
       self.player.on('play', self.handlePlay);
   });

如果这行代码是你的错误而你想写self.state.player = this而不是self.player,请注意这不会更新状态。