I have a simple React app with a video player and chart displaying data about the video. Both are in their own components at the top level:
class App extends Component {
...
render() {
return (
<div className="App">
<VideoDisplay .../>
<MetricsDisplay .../>
</div>
)
}
}
Inside VideoDisplay.js
is a <video>
element that I want to control, specifically by seeking its play position, which I can do using videoElement.currentTime=seekToTime
. I want to control this from the MetricsDisplay
element: if I click on the graph I want the video to seek to the time clicked on. But to do that it seems I have to call a function in VideoDisplay.js
from App.js
when it receives the click from MetricsDisplay.js
. But this is imperative instead of declarative, and indeed I'm having trouble implementing this in React. Is it even possible? How else could I tell the video in an element to seek, triggered by a click from a sibling element?
I have a workaround that I'll post as an answer but I'm very convinced it's pretty sub-optimal from a design perspective, and will cause code maintenance headaches. So what's a better method? What the "React" way to do something like this?
See attached screenshot: when clicking on the graph in the right, the video should seek to the clicked point in time.