简体   繁体   English

另一个初学者 React 问题 - QRCode Generation

[英]Another beginner React question - QRCode Generation

Essentially, I want to generate a QR Code in a ReactJS application.本质上,我想在 ReactJS 应用程序中生成一个二维码。 I found a generator on npm for React & React Native.我在 npm 上找到了一个用于 React 和 React Native 的生成器。 Here is how they set it up on the npm site:以下是他们在 npm 网站上的设置方式:

import React from "react";
import ReactDOM from "react-dom";
import QRCode from "react-qr-code";
 
ReactDOM.render(<QRCode value="hey" />, document.getElementById("Container"));

Here is how I set up the page that will hold the QR Code:以下是我如何设置将保存 QR 码的页面:

import React from "react";
import ReactDOM from "react-dom";
import QRCode from "react-qr-code";

// empty profile page
class Profile extends React.Component 
{
  render() 
  {
    return (
      <div>
          <h1>QR Code Page</h1>
      </div>
  );
}

} 
//ReactDOM.render(<QRCode value="hey" />, document.getElementById("Container"));
export default Profile; 

I have the actual QRCode code commented out, as I'm not sure where to put it in the code so that it works & shows up on the page.我已将实际的 QRCode 代码注释掉,因为我不确定将其放在代码中的哪个位置以便它工作并显示在页面上。 When I put it anywhere inside the Profile class the line has red underlines stating that a ";"当我将它放在配置文件 class 中的任何位置时,该行带有红色下划线,表示“;” or ")" is expected (depending on where it's put I get ";" or ")").或“)”是预期的(取决于它放在哪里我得到“;”或“)”)。 I know I'm not missing either, so I'm pretty sure it comes down to where I'm putting the line of code to generate the QRCode.我知道我也没有错过,所以我很确定它归结为我放置代码行以生成 QRCode 的位置。

I'm sorry if this is is an obvious question, I'm still pretty new to React.如果这是一个明显的问题,我很抱歉,我对 React 还是很陌生。 Please let me know if you need any more information!如果您需要更多信息,请告诉我!

Try this.尝试这个。

        import React from "react";
        import QRCode from "react-qr-code";
        
        // empty profile page
        class Profile extends React.Component     {
          render() {
            return (
              <div>
                  <h1>QR Code Page</h1>
                  <QRCode value="hey" />
              </div>
          );
         }
        } 
        export default Profile; 

you can use it return itself it will work你可以使用它自己返回它会工作

return (
          <div>
              <h1>QR Code Page</h1>
              <QRCode value="hey" />
          </div>)

You can just use the <QRCode value="hey"></QRCode> .您可以只使用<QRCode value="hey"></QRCode> You also don't need dom library for this.您也不需要 dom 库。 So I've removed it.所以我把它删除了。

import React from "react";
import QRCode from "react-qr-code";

// empty profile page
class Profile extends React.Component 
{
  render() 
  {
    return (
      <div>
          <h1>QR Code Page</h1>
          <QRCode value="hey"></QRCode>
      </div>
  );
}

} 
export default Profile; 

Check out this live version to see it in action https://codesandbox.io/s/billowing-butterfly-0dr39?file=/src/App.js查看此实时版本以查看其实际效果 https://codesandbox.io/s/billowing-butterfly-0dr39?file=/src/App.js

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

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