首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ReactJS中渲染Clappr播放器

在ReactJS中渲染Clappr播放器
EN

Stack Overflow用户
提问于 2016-08-19 04:48:15
回答 2查看 1.7K关注 0票数 2

我将Clappr播放器ReactJS结合使用。

当我点击一个切换按钮时,我希望Clappr组件出现并销毁。但是,当创建Clappr player时,整个页面都会重新加载(切换按钮会在眨眼之间显示出来)。这是我的密码:

ClapprComponent.js

代码语言:javascript
复制
import React, { Component } from 'react'
import Clappr from 'clappr'

class ClapprComponent extends Component {
    shouldComponentUpdate(nextProps) {
        let changed = (nextProps.source != this.props.source)
        if (changed) {
            this.change(nextProps.source)
        }
        return false
    }
    componentDidMount() {
        this.change(this.props.source)
    }
    componentWillUnmount() {
        this.destroyPlayer()
    }
    destroyPlayer = () => {
        if (this.player) {
            this.player.destroy()
        }
        this.player = null
    }
    change = source => {
        if (this.player) {
            this.player.load(source)
            return
        }
        const { id, width, height } = this.props
        this.player = new Clappr.Player({
            baseUrl: "/assets/clappr",
            parent: `#${id}`,
            source: source,
            autoPlay: true,
            width: width,
            height: height
        })
    }
    render() {
        const { id } = this.props
        return (
            <div id={id}></div>
        )
    }
}

export default ClapprComponent

Video.js

代码语言:javascript
复制
import React, { Component } from 'react'

import { Clappr } from '../components'

class VideoList extends Component {
    constructor() {
        super()
        this.state = {
            isActive: false
        }
    }
    toggle() {
        this.setState({
            isActive: !this.state.isActive
        })
    }
    render() {
        const boxStyle = {
            width: "640",
            height: "360",
            border: "2px solid",
            margin: "0 auto"
        }
        return (
            <div>
                <div style={boxStyle}>
                    {this.state.isActive ?
                        <Clappr
                            id="video"
                            source="http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"
                            width="640"
                            height="360" />
                    : ''}
                </div>
                <button class="btn btn-primary" onClick={this.toggle.bind(this)}>Toggle</button>
            </div>
        )
    }
}

export default VideoList

有人能解释原因吗?如何解决这个问题?

编辑1:我有点理解为什么要重新加载按钮。这是因为在index.html <head>中,我加载了一些css。当页面重新呈现时,它首先加载css,然后执行我的app.min.js。如果我将css标签移到<script src="app.min.js"></script>下,按钮不会在眨眼之间重新加载。但这还不能解决我的问题。因为css文件必须放入<head>标记。有什么帮助吗?

EN

回答 2

Stack Overflow用户

发布于 2016-08-19 08:11:05

这里有一个运行(jsbin链接)示例。我简化了一点,它仍然显示了您的主要需求:

代码语言:javascript
复制
class ClapprComponent extends React.Component {

  componentDidMount(){

    const { id, source } = this.props;

    this.clappr_player = new Clappr.Player({
      parent: `#${id}`,
      source: source
    });

  }

  componentWillUnmount() {
    this.clappr_player.destroy();
    this.clappr_player = null;
  }

  render() {

    const { id } = this.props;

    return (
      <div>
        <p>Active</p>
        <p id={id}></p>
      </div>
    );
  }

}

class NotActive extends React.Component {

  render() {

    return (
      <p>Not Active</p>
    );
  }
}

class App extends React.Component {

  constructor(props){

    super(props);

    this.toggle = this.toggle.bind(this);

    this.state = {
      isActive: false
    }
  }

  toggle() {

    this.setState({
      isActive: !this.state.isActive
    })
  }  

  render() {

    return (
      <div>

        <h1>Clappr React Demo</h1>

        { this.state.isActive ? 
            <ClapprComponent 
              id="video"
              source="http://www.html5videoplayer.net/videos/toystory.mp4"
            /> : 
            <NotActive />}

        <button onClick={this.toggle}>Toggle</button>

      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('app'));

还要确保将按钮class属性重命名为className

也许你可以在这里工作找出你的确切问题?希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2016-08-19 11:43:08

在Clappr的文档中,我发现了一个关于如何在reactjs中使用clappr的相似之处

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39031308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档