I'm working on embedding YT videos into the video component view using this tutorial as a guide:
https://reactnativecode.com/find-replace-string-text-component/
It works but I have to replace part of the string manually to display it on the page when I paste in the URL.
Raw Youtube URL
https://www.youtube.com/watch?v=6ZhJjRBnWYk
Manually typed
https://www.youtube.com/embed/6ZhJjRBnWYk
For aesthetic there's no submit button so when the url is pasted in, it automatically renders the video. Normally, the onClick would handle the fixURLString function.
I'm trying to replace the string but it's not working because I'm not sure how to handle the fixURLString without a button as shown in the tutorial.
export default class VideoEmbed extends Component {
constructor(props) {
super(props)
this.state = {
embeds: [],
url: '',
}
}
...
onChange = (e) => {
e.preventDefault()
this.setState({
[e.target.name]: e.target.value,
})
}
fixUrlString = () => {
const urlString = this.state.url.toString()
const newText = urlString.replace('/watch?v=', '/embed/')
this.setState({ url: newText })
}
...
<div className="upload-image-box">
<form>
<input
name="url"
type="text"
placeholder="paste YouTube embed code here"
value={this.state.url}
onChange={(e) => this.onChange(e)}
/>
</form>
</div>
...