Skip to content

Commit

Permalink
Use ref callbacks instead of ref strings
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Sep 25, 2016
1 parent ab24d08 commit 6c16ba0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ export default class ReactPlayer extends Component {
)
}
seekTo = fraction => {
const player = this.refs.player
if (player) {
player.seekTo(fraction)
if (this.player) {
this.player.seekTo(fraction)
}
}
progress = () => {
if (this.props.url && this.refs.player) {
const loaded = this.refs.player.getFractionLoaded() || 0
const played = this.refs.player.getFractionPlayed() || 0
if (this.props.url && this.player) {
const loaded = this.player.getFractionLoaded() || 0
const played = this.player.getFractionPlayed() || 0
const progress = {}
if (loaded !== this.prevLoaded) {
progress.loaded = loaded
Expand Down Expand Up @@ -80,7 +79,8 @@ export default class ReactPlayer extends Component {
renderPlayer = Player => {
const active = Player.canPlay(this.props.url)
const { youtubeConfig, soundcloudConfig, vimeoConfig, fileConfig, ...activeProps } = this.props
const props = active ? { ...activeProps, ref: 'player' } : {}
const ref = player => { this.player = player }
const props = active ? { ...activeProps, ref } : {}
return (
<Player
key={Player.displayName}
Expand Down
12 changes: 6 additions & 6 deletions src/demo/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class App extends Component {
}
onSeekMouseUp = e => {
this.setState({ seeking: false })
this.refs.player.seekTo(parseFloat(e.target.value))
this.player.seekTo(parseFloat(e.target.value))
}
onProgress = state => {
// We only want to update time slider if we are not currently seeking
Expand All @@ -53,7 +53,7 @@ export default class App extends Component {
onConfigSubmit = () => {
let config
try {
config = JSON.parse(this.refs.config.value)
config = JSON.parse(this.configInput.value)
} catch (error) {
config = {}
console.error('Error setting config:', error)
Expand Down Expand Up @@ -83,7 +83,7 @@ export default class App extends Component {
<section className='section'>
<h1>ReactPlayer Demo</h1>
<ReactPlayer
ref='player'
ref={player => { this.player = player }}
className='react-player'
width={480}
height={270}
Expand Down Expand Up @@ -181,14 +181,14 @@ export default class App extends Component {
<tr>
<th>Custom URL</th>
<td>
<input ref='url' type='text' placeholder='Enter URL' />
<button onClick={() => this.setState({ url: this.refs.url.value })}>Load</button>
<input ref={input => { this.urlInput = input }} type='text' placeholder='Enter URL' />
<button onClick={() => this.setState({ url: this.urlInput.value })}>Load</button>
</td>
</tr>
<tr>
<th>Custom config</th>
<td>
<textarea ref='config' placeholder='Enter JSON' />
<textarea ref={textarea => { this.configInput = textarea }} placeholder='Enter JSON' />
<button onClick={this.onConfigSubmit}>Update Config</button>
</td>
</tr>
Expand Down
3 changes: 1 addition & 2 deletions src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default class FilePlayer extends Base {
return true
}
componentDidMount () {
this.player = this.refs.player
this.player.addEventListener('canplay', this.onReady)
this.player.addEventListener('play', this.onPlay)
this.player.addEventListener('pause', this.props.onPause)
Expand Down Expand Up @@ -69,7 +68,7 @@ export default class FilePlayer extends Base {
}
return (
<Media
ref='player'
ref={player => { this.player = player }}
style={style}
preload='auto'
controls={controls}
Expand Down
2 changes: 1 addition & 1 deletion src/players/SoundCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class SoundCloud extends FilePlayer {
return (
<div style={style}>
<audio
ref='player'
ref={player => { this.player = player }}
type='audio/mpeg'
preload='auto'
style={{ width: '100%', height: '100%' }}
Expand Down
10 changes: 8 additions & 2 deletions src/players/Vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default class Vimeo extends Base {
}
componentDidMount () {
window.addEventListener('message', this.onMessage, false)
this.iframe = this.refs.iframe

if (!this.props.url && this.props.vimeoConfig.preload) {
this.preloading = true
Expand Down Expand Up @@ -109,6 +108,13 @@ export default class Vimeo extends Base {
width: '100%',
height: '100%'
}
return <iframe ref='iframe' frameBorder='0' style={style} allowFullScreen={fullscreen} />
return (
<iframe
ref={iframe => { this.iframe = iframe }}
frameBorder='0'
style={style}
allowFullScreen={fullscreen}
/>
)
}
}

0 comments on commit 6c16ba0

Please sign in to comment.