1

我尝试测试 getUserConfirmation 的功能。代码如下:

getUserConfirmation={(message, callback) => {
          const container = document.createElement('div');
          // callback(false);
          document.body.append(container);
          ReactDOM.render(
            <div
              style={{
                width: '400px',
                height: '400px',
                backgroundColor: 'black',
                zIndex: 1000,
              }}
            />,
            container,
            console.log('Here'),
          );
        }}
      >

代码对我更改 url 和回调 (console.log('here')) 的尝试作出反应。但是浏览器中什么也没有出现。我做错了什么?

4

2 回答 2

0

我添加了位置:'absolute',现在我可以看到我的 div。在我“隐藏”在脚本标签下之前。哈哈

于 2020-09-22T04:17:17.033 回答
0

它适用于这个例子:

const getUserConfirmation = (message, callback) => {
  const container = document.createElement("div");
  // callback(false);
  document.body.append(container);
  ReactDOM.render(
    <div
      style={{
        width: "400px",
        height: "400px",
        backgroundColor: "black",
        zIndex: 1000
      }}
    />,
    container,
    console.log("Here")
  );
};

getUserConfirmation();
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <title>React App</title>
</head>

<body>
    <noscript>
        You need to enable JavaScript to run this app.
    </noscript>
</body>

</html>

然而,这样做的目的是什么?为什么不在 html 中有一个固定的“根 div”并将这个元素传递给 ReactDOM.render?

于 2020-09-22T03:12:50.450 回答