Consider the following simple React component that displays a YouTube video and has a button to change the video:
import React from 'react';
import ReactDOM from 'react-dom';
class Video extends React.Component {
state = { video: 'G8KpPw303PY' };
handleClickButton = event => {
this.setState({ video: 'PHAc3_MEjgQ' });
};
render() {
return (
<div>
<iframe
src={`https://www.youtube.com/embed/${this.state.video}?autoplay=1`}
/>
<input
type="button"
value="Change Video"
onClick={this.handleClickButton}
/>
</div>
);
}
}
ReactDOM.render(<Video />, document.getElementById('react-root'));
If I were to click on the button, the video changes (as it should). However, a page is also added to the browser history. How do I make it so a page is not added to the browser history?
I understand that to load a new URL in an iframe in vanilla JavaScript, we'd use iframe.contentWindow.location.replace('url'). Due to React's declarative/functional nature though, doing something like that seems really hacky, and I understand there would need to be built-in support for something like this.