简体   繁体   English

React js tsx codesandbox问题 抛出了跨域错误,lodash.isequal

[英]React js tsx codesandbox problem A cross-origin error was thrown and lodash.isequal

I was trying the following code, of a module, but it gives me the following problems:我正在尝试一个模块的以下代码,但它给我带来了以下问题:

Problem:问题:

A cross-origin error was thrown.引发了跨域错误。 React doesn't have access to the actual error object in development. React 无法访问开发中的实际错误 object。 See.../react-crossorigin-error for more information.有关更多信息,请参阅.../react-crossorigin-error。

Problem:问题:

module "/sandbox/node_modules/@types/lodash.isequal/index" This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.ts(2497)模块“/sandbox/node_modules/@types/lodash.isequal/index”这个模块只能通过打开'esModuleInterop'标志并引用它的默认export.ts(2497)来引用ECMAScript导入/导出

Link: Codesandbox链接: Codesandbox

Code:代码:

import * as isEqual from "lodash.isequal";
import * as qrGenerator from "qrcode-generator";
import * as React from "react";
import * as ReactDOM from "react-dom";

export interface IProps {
  value?: string;
  ecLevel?: "L" | "M" | "Q" | "H";
  enableCORS?: boolean;
  size?: number;
  quietZone?: number;
  bgColor?: string;
  fgColor?: string;
  logoImage?: string;
  logoWidth?: number;
  logoHeight?: number;
  logoOpacity?: number;
  qrStyle?: "squares" | "dots";
  style?: object;
}

export class QRCode extends React.Component<IProps, {}> {
  private canvas: React.RefObject<HTMLCanvasElement>;

  public static defaultProps: IProps = {
    value: "https://reactjs.org/",
    ecLevel: "M",
    enableCORS: false,
    size: 150,
    quietZone: 10,
    bgColor: "#FFFFFF",
    fgColor: "#000000",
    logoOpacity: 1,
    qrStyle: "squares"
  };

  private static utf16to8(str: string): string {
    let out: string = "",
      i: number,
      c: number;
    const len: number = str.length;
    for (i = 0; i < len; i++) {
      c = str.charCodeAt(i);
      if (c >= 0x0001 && c <= 0x007f) {
        out += str.charAt(i);
      } else if (c > 0x07ff) {
        out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f));
        out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
      } else {
        out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
      }
    }
    return out;
  }

  private drawPositioningPattern(row, col, length, ctx) {
    const cellSize = this.props.size / length;
    for (let r = -1; r <= 7; r++) {
      if (!(row + r <= -1 || length <= row + r)) {
        for (let c = -1; c <= 7; c++) {
          if (
            (!(col + c <= -1 || length <= col + c) &&
              (0 <= r && r <= 6 && (c === 0 || c === 6))) ||
            (0 <= c && c <= 6 && (r === 0 || r === 6)) ||
            (2 <= r && r <= 4 && 2 <= c && c <= 4)
          ) {
            const w =
              Math.ceil((row + r + 1) * cellSize) -
              Math.floor((row + r) * cellSize);
            const h =
              Math.ceil((col + c + 1) * cellSize) -
              Math.floor((col + c) * cellSize);

            ctx.fillStyle = this.props.fgColor;
            ctx.fillRect(
              Math.round((row + r) * cellSize),
              Math.round((col + c) * cellSize),
              w,
              h
            );
          }
        }
      }
    }
  }

  constructor(props: IProps) {
    super(props);
    this.canvas = React.createRef();
  }

  shouldComponentUpdate(nextProps: IProps) {
    return !isEqual(this.props, nextProps);
  }

  componentDidMount() {
    this.update();
  }

  componentDidUpdate() {
    this.update();
  }

  update() {
    const {
      value,
      ecLevel,
      enableCORS,
      size,
      bgColor,
      fgColor,
      logoImage,
      logoWidth,
      logoHeight,
      logoOpacity,
      qrStyle
    } = this.props;

    const qrCode = qrGenerator(0, ecLevel);
    qrCode.addData(QRCode.utf16to8(value));
    qrCode.make();

    const canvas: HTMLCanvasElement = ReactDOM.findDOMNode(
      this.canvas.current
    ) as HTMLCanvasElement;
    const ctx: CanvasRenderingContext2D = canvas.getContext("2d");

    const length = qrCode.getModuleCount();
    const cellSize = size / length;
    const scale = window.devicePixelRatio || 1;
    canvas.height = canvas.width = size * scale;
    ctx.scale(scale, scale);

    ctx.fillStyle = bgColor;
    ctx.fillRect(0, 0, size, size);

    if (qrStyle === "dots") {
      ctx.fillStyle = fgColor;
      const radius = cellSize / 2;
      for (let row = 0; row < length; row++) {
        for (let col = 0; col < length; col++) {
          if (qrCode.isDark(row, col)) {
            ctx.beginPath();
            ctx.arc(
              Math.round(col * cellSize) + radius,
              Math.round(row * cellSize) + radius,
              (radius / 100) * 75,
              0,
              2 * Math.PI,
              false
            );
            ctx.closePath();
            ctx.fill();
          }
        }
      }

      this.drawPositioningPattern(0, 0, length, ctx);
      this.drawPositioningPattern(length - 7, 0, length, ctx);
      this.drawPositioningPattern(0, length - 7, length, ctx);
    } else {
      for (let row = 0; row < length; row++) {
        for (let col = 0; col < length; col++) {
          if (qrCode.isDark(row, col)) {
            ctx.fillStyle = fgColor;
            const w =
              Math.ceil((col + 1) * cellSize) - Math.floor(col * cellSize);
            const h =
              Math.ceil((row + 1) * cellSize) - Math.floor(row * cellSize);
            ctx.fillRect(
              Math.round(col * cellSize),
              Math.round(row * cellSize),
              w,
              h
            );
          }
        }
      }
    }

    if (logoImage) {
      const image = new Image();
      if (enableCORS) {
        image.crossOrigin = "Anonymous";
      }
      image.onload = () => {
        const dwidth = logoWidth || size * 0.2;
        const dheight = logoHeight || dwidth;
        const dx = (size - dwidth) / 2;
        const dy = (size - dheight) / 2;
        image.width = dwidth;
        image.height = dheight;
        ctx.save();
        ctx.globalAlpha = logoOpacity;
        ctx.drawImage(image, dx, dy, dwidth, dheight);
        ctx.restore();
      };
      image.src = logoImage;
    }
  }

  render() {
    return React.createElement("canvas", {
      id: "react-qrcode-logo",
      height: this.props.size,
      width: this.props.size,
      style: {
        height: this.props.size + "px",
        width: this.props.size + "px",
        padding: this.props.quietZone + "px",
        background: this.props.bgColor
      },
      ref: this.canvas
    });
  }
}

I see only the cross-origin thrown error in your sandbox.我在您的沙箱中只看到了跨域引发的错误。 I've seen this cross-origin error quite a few times in codesandbox, I believe it is because it is running in an iframe, and is harmless as far as I can tell.我在代码沙箱中多次看到这个跨域错误,我相信这是因为它在 iframe 中运行,据我所知是无害的。

I think just using a default import will fix your second error, does for me in your sandbox.我认为仅使用默认导入将修复您的第二个错误,在您的沙箱中对我有用。

import isEqual from "lodash.isequal";

暂无
暂无

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

相关问题 Codesandbox.io 引发了跨域错误问题 - A cross-origin error was thrown problem with Codesandbox.io 未捕获的错误:在 React 中抛出跨源错误 - Uncaught Error: A cross-origin error was thrown in React 引发了跨域错误。 react 无权访问 react 开发中的实际错误 object - A cross-origin error was thrown. react doesn't have access to the actual error object in development in react 错误:引发了跨域错误。 React 无法访问开发中的实际错误 object - Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development × 未处理的拒绝(错误):抛出了跨域错误。 React 无权访问开发中的实际错误对象 - × Unhandled Rejection (Error): A cross-origin error was thrown. React doesn't have access to the actual error object in development "<i>Uncaught Error: A cross-origin error was thrown.<\/i>未捕获的错误:引发了跨域错误。<\/b> <i>React doesn&#39;t have access to the actual error object in development<\/i> React 无法访问开发中的实际错误对象<\/b>" - Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development getImageData跨源错误 - getImageData cross-origin error 跨域请求错误 - cross-origin request ERROR json.parse 触发 React / MERN 中的跨域错误 - json.parse triggers cross-origin error in React / MERN 更新 React state 变量会引发跨域错误 - Updating a React state variable throws cross-origin error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM