I'm making a small Video component in React (for you guessed it, playing videos) and I want to embed that component inside a parent component and then be able to call a play method on the video component.
My video component looks like:
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
const { string, func } = PropTypes;
export default class Video extends Component {
static propTypes = {
source: string.isRequired,
type: string.isRequired,
className: string
};
play = () => {
};
render = () => {
const { className } = this.props;
return (
<video className={ className } width="0" height="0" preload="metadata">
<source src={ this.props.source } type={ this.type } />
Your browser does not support the video tag.
</video>
);
};
}
It's really simple, nothing fancy going on here.
Now in the parent component, lets call it Page:
export default class Page extends Component {
video = (
<Video source="some_url" type="video/mp4" />
);
render = () => {
<div onClick={ this.video.play } />
}
}
However if I log .play it's undefined.
Next I tried declaring play as a prop in Video and putting a default prop like:
static defaultProps = {
play: () => {
const node = ReactDOM.findDOMNode(this);
}
}
But in this context, this in undefined.
What is the proper way to expose a function on a React ES6 class so that it can be called by external components? Should I attach something to Video.prototype?