my goal is to time the visibility of child components based on the playback time of a video. This components, called InfoElement, are rendered thanks to map method.
{this.props.sceneNavigator.viroAppProps.tours[this.state.tourId].scenes[this.state.sceneId].infoElements.map((infoElement, key) => {
return (
<InfoElement
content={{uri: infoElement.poiImage}}
startingTime = {infoElement.startingTime}
endingTime = {infoElement.endindTime}
contentCardScale={[5, 5, 5]}
position={polarToCartesian(infoElement.coordinate)}/>
)
})}
Inside the parent, there is a callback, _onUpdateTime(currentPlaybackTimeInSeconds, totalPlayBackDurationInSeconds) that is called when the current playback position of the video has changed.
<Viro360Video
source={{uri:this.props.sceneNavigator.viroAppProps.tours[this.state.tourId].scenes[this.state.sceneId].backgroundSource}}
onBufferEnd={this.onLoadEnded}
onUpdateTime={this._onUpdateTime}
rotation={[0, 0, 0]}
onError={this.onError}/>
_onUpdateTime(currentPlaybackTimeInSeconds, totalPlayBackDurationInSeconds) {
this.setState({
visible : false,
});
console.log(currentPlaybackTimeInSeconds);
}
What i want to do is to compare the currentPlaybackTimeInSeconds with startingTime and endingTime and make the infoElement component visible only ifcurrentPlaybackTimeInSeconds is between startingTime and endingTime. My problem is that i don't have just one infoElement, but a list, so i don't know how to refer to each of them. The idea would be to update the state of each of them, but i have no idea how to do that. Any advice? Thanks to all that will answer me.
You need to inverse your thinking on this one. What you should be doing is telling each InfoElement what the current time is, and then they change their rendered output based on that time. Something like this:
In this, you can see that we're hooking to the
Viro360VideoonUpdateTimeand setting acurrentTimestate. We then pass thecurrentTimeto eachInfoElement. Inside theInfoElementwe check thecurrentTimepassed in against thestartingTimeandendingTime, if we're inside those bounds, we render our elements, otherwise, we return null.Notes
<></>) instead of adivor aViewsince I don't know if you're targeting React Native or ReactDOM or some other rendering engine.