gpt4 book ai didi

reactjs - 如何获取用户输入(YouTube视频链接)并使用React.js将其嵌入为iframe?

转载 作者:行者123 更新时间:2023-12-03 05:55:47 28 4
gpt4 key购买 nike

我目前正在尝试开发一个网络应用程序,该应用程序可以接受用户输入(即YouTube™视频网址)并输出该视频的iframe。我能够使用基本的HTML和JavaScript产生此结果,但是现在我必须将其转换为React,所以我无法弄清楚我在做什么错。

这是一些相关的代码。

app.js


var React = require('react');
var ReactDOM = require('react-dom');

var Header = React.createClass({
render: function () {
return (
<header>
<h1>{this.props.title}</h1>
</header>
);
}
});

var createIframeLink = function (link) {
if (link.charAt(12) == 'y') { //if the 13th character = y (youtube videos)
var number = link.substring(32); //key # = from 33rd character on
var embed = "https://www.youtube.com/embed/" + number; //Add youtube link before key #
return embed
}
else if (link.charAt(12) == 'o') { //if the 13th character = o (vimeo videos)
var number = link.substring(18); //key # = from 19th character on
var embed = "https://player.vimeo.com/video/" + number; //Add vimeo link before key #
return embed
}
else {}
};

var iframevideo = React.createClass({
render: function() {
return (
<div id="iframe">
<iframe id="iframe" src="this.props.embed" frameborder="0"></iframe>
</div>
)
}
});

var iframeblock = React.createClass({
render: function() {
var link = this.state.link;
return (
<div className="form">
<div>
<label>Principal:</label>
<input type="text" value={this.state.link} onChange={this.createIframeLink}/>
</div>
</div>
<iframe id="iframe" src="this.props.embed" frameborder="0"></iframe>
)
}
});

var App = React.createClass({
render: function () {
return (
<div>
<Header title="Vidvision"/>
<iframeblock/>
</div>
);
}
});

ReactDOM.render(<App/>, document.getElementById("app"));

index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href='http://fonts.googleapis.com/css?family=Roboto:400,300,700' rel='stylesheet' type='text/css'>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="app"></div>
<script src="build/app.bundle.js"></script>
</body>
</html>

当我运行我的应用程序时,出现错误:

(SyntaxError: ... Adjacent JSX elements must be wrapped in an enclosing tag (48:12).



这是正常的HTML和JavaScript代码:
<div class="myIframe">
<iframe id="iframe" src="" frameborder="0" &fs=0></iframe>
</div>

<script type="text/javascript">
function createYouTubeEmbedLink (link) {
link = document.getElementById('link').value;
if (link.charAt(12) == 'y') { //if the 13th character = y (youtube videos)
var number = link.substring(32); //key # = from 33rd character on
var embed = "https://www.youtube.com/embed/" + number; //Add youtube link before key #
document.getElementById('iframe').src = embed;
}
else if (link.charAt(12) == 'o') { //if the 13th character = o (vimeo videos)
var number = link.substring(18); //key # = from 19th character on
var embed = "https://player.vimeo.com/video/" + number; //Add vimeo link before key #
document.getElementById('iframe').src = embed;
}
else {}
}
</script>

最佳答案

您收到的错误是因为Component iframeblock无法返回两个元素,因此您需要将<div><iframe>包装在另一个<div>

var iframeblock = React.createClass({
render: function() {
var link = this.state.link;
return (
<div>
<div className="form">
<div>
<label>Principal:</label>
<input type="text" value={this.state.link} onChange={this.createIframeLink}/>
</div>
</div>
<iframe id="iframe" src="this.props.embed" frameborder="0"></iframe>
</div>
);
}
});

关于reactjs - 如何获取用户输入(YouTube视频链接)并使用React.js将其嵌入为iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41269685/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com