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

Calling the method of React VideoJS component from another component

本文关键字:组件 方法 VideoJS 调用 另一个 React      更新时间:2023-09-26

我在我的react项目中有一个react VideoJs组件与标记。我有一个叫做jumpToSpecificMarker的函数。我有另一个组件havejumpToSpecificMarker,它有这些标记的名称。所有我想做的是调用jumpToSpecificMarker每当我点击listview的listItem。目前,我在onTouchTap事件中将action传递给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);

您在哪里使用您在此代码块中定义的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,请注意,这不会更新状态