简体   繁体   English

使用用户输入响应更改 iframe src

[英]React change iframe src with user input

I want to be able to change an iframe src with the help of react but i don't know were to start as i am new to coding in general.我希望能够在 react 的帮助下更改 iframe src,但我不知道要开始,因为我通常是编码新手。

I manage to build what i want with html and js but it is not working when i dump the code in a react project.我设法用 html 和 js 构建我想要的东西,但是当我将代码转储到 react 项目中时它不起作用。

edit: I tried converting html and js to jsx with an online convertor but it did not work.编辑:我尝试使用在线转换器将 html 和 js 转换为 jsx,但它不起作用。

Please see how code below works and point me to the right direction please.请查看下面的代码如何工作,并请指出正确的方向。

 <input type="number" id="book"></input> <input type="number" id="page"></input> <button type="button" onClick="goBook();">Submit</button> <iframe id="myBook" src="https://www.google.com" height="720" width="1280" frameborder="0" scrolling="no" allowfullscreen="true"> </iframe> <script> function goBook(){ document.getElementById("myBook").src = "https://www.example.com/b="+document.getElementById("book").value+"&p="+document.getElementById("page").value; } </script>

please someone help me!请有人帮助我!

You need to start with create-react-app.您需要从 create-react-app 开始。

After you get the basic react application template, Go to App.js and write this code:在你得到基本的 react 应用模板后,Go 到 App.js 并编写以下代码:

import "./styles.css";
import { useState } from "react";

export default function App() {
  const [srcURL, setsrcURL] = useState("https://www.google.com");
  const [book, setBook] = useState(null);
  const [page, setPage] = useState(null);

  const handleSubmit = () => {
    if (book && page) {
      setsrcURL(`${"https://www.google.com"}/b=${book}&p=${page}`);
    }
  };

  return (
    <div className="App">
      <input
        type="number"
        name="book"
        placeholder="Book Number"
        onChange={(e) => setBook(e.target.value)}
      />
      <input
        type="number"
        placeholder="Page Number"
        name="page"
        onChange={(e) => setPage(e.target.value)}
      />
      <button type="button" onClick={handleSubmit}>
        Submit
      </button>
      <iframe
        name="myBook"
        src={srcURL}
        height="720"
        width="1280"
        frameBorder="0"
        scrolling="no"
        allowFullScreen={true}
      />
    </div>
  );
}

My Answer was deleted so.我的答案被删除了。 I'll just copy paste here.我这里只是复制粘贴。

import { useRef, useState } from "react";

export default function App() {
  const googleUrl = "https://www.google.com";

  const refBook = useRef(null);
  const refPage = useRef(null);
  const refIframe = useRef(null);
  const [iframSrc, setIframeUrl] = useState(googleUrl);

  const goBook = () => {
    setIframeUrl(
      `${googleUrl}/b=${refBook.current.value}&p=${refPage.current.value}`
    );
  };

  return (
    <div className="App">
      <input type="number" ref={refBook}></input>
      <input type="number" ref={refPage}></input>
      <button type="button" onClick={goBook}>
        Submit
      </button>
      <iframe
        title="myBook"
        src={iframSrc}
        height="720"
        width="1280"
        frameBorder="0"
        scrolling="no"
        allowFullScreen={true}
        ref={refIframe}
      ></iframe>
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM