In my react component I have a video tag like so:
<video>
<source src="https://fake.com/file.mp4" type="video/mp4" />
</video>
And I have a button a bit further down that I would like to use to activate the video:
<button onClick={playVideoFunc?}/>
I assume I will be using some sort of on click event to trigger the video play and I know I can use the following javascript code to do it:
var v = document.getElementsByTagName("video")[0];
v.play();
But sense this is my first time using react I was wondering if there was a more "reacty" way of doing this? Additionally if I do have to use pure javascript like above is it appropriate to put that code inside the react component itself? For reference my component looks like this (minus some details):
import React, { Component } from 'react';
import Footer from './footerComponent'
import Navbar from './navbarComponent';
class Home extends React.Component{
render() {
return (
<div className="wrapper home-page">
<div className="jumbotron-video-wrapper">
<video>
<source src="https://fake.com/file.mp4" type="video/mp4" />
</video>
</div>
<Navbar type={"transparent"}/>
<div className="jumbotron margin-bottom-20" style={heroStyle}>
<div className="container-fluid">
<div className="row">
<div className="col-md-8 col-md-offset-2 text-center">
<h1>WORDS WORDS WORDS</h1>
<img onClick={playvideo??} width="130" src={playImage}/>
<p className="thin">Watch our video</p>
</div>
</div>
</div>
</div>
<Footer/>
</div>
)
}
};
export default Home;
The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as a button, in your app. Event names are written in camelCase, so the onclick event is written as onClick in a React app. In addition, React event handlers appear inside curly braces.
If you've created your project via create-react-app , then you will want to create a Videos folder under the public directory, and put your video file there, seeing this is where public assets are served from by default. So, put your video file in my-app/public/Videos/video1.
To set your mind at ease, the onClick event does work with divs in react, so double-check your code syntax.
As for whether it mimics HTML4 or HTML5, it depends on what you code, React itself is not limited to any HTML version and it is the browser that needs to worry about which HTML version you are using.
You can assign a ref to your video element and call play() using this.refs.video.play().
class Home extends React.Component{
render() {
return (
<div className="wrapper home-page">
<div className="jumbotron-video-wrapper">
<video ref="video">
<source src="https://fake.com/file.mp4" type="video/mp4" />
</video>
</div>
<Navbar type={"transparent"}/>
<div className="jumbotron margin-bottom-20" style={heroStyle}>
<div className="container-fluid">
<div className="row">
<div className="col-md-8 col-md-offset-2 text-center">
<h1>WORDS WORDS WORDS</h1>
<img onClick={() => {this.refs.video.play()}} width="130" src={playImage}/>
<p className="thin">Watch our video</p>
</div>
</div>
</div>
</div>
<Footer/>
</div>
)
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With