如何使我的App.js文件正常运行?

huangapple go评论98阅读模式
英文:

How do I get my App.js file to run properly?

问题

I am new to coding, so I am following this tutorial: https://www.sitepoint.com/react-tutorial-build-calculator-app/ I've been unable to render any elements. This is what the console shows when running the web app.

This is the code I have for my App.js:

import Wrapper from "./components/Wrapper.js";
import Screen from "./components/Screen.js";
import ButtonBox from "./components/ButtonBox.js";
import Button from "./components/Button.js";

const btnValues = [
  ["C", "+-", "%", "/"],
  [7, 8, 9, "X"],
  [4, 5, 6, "-"],
  [1, 2, 3, "+"],
  [0, ".", "="],
];

const App = () => {
  return (
    <Wrapper>
      <Screen value="0" />
      <ButtonBox>
        {
          btnValues.flat().map((btn, i) => {
            return (
              <Button
                key={i}
                className={btn === "=" ? "equals" : ""}
                value={btn}
                onClick={() => {
                  console.log(`${btn} clicked!`);
                }}
              />
            );
          })
        }
      </ButtonBox>
    </Wrapper>
  );
};

export default App;

Then this is the index.js:

import React from 'react';
import ReactDOM from 'react-dom';

import './index.css';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);
英文:

I am new to codding, so I am following this tutorial: https://www.sitepoint.com/react-tutorial-build-calculator-app/ I've been unable to render any elements. This is what the console shows when running the web app

如何使我的App.js文件正常运行?

This is the code I have for my App.js:

import Wrapper from &quot;./components/Wrapper.js&quot;;
import Screen from &quot;./components/Screen.js&quot;;
import ButtonBox from &quot;./components/ButtonBox.js&quot;;
import Button from &quot;./components/Button.js&quot;;

const btnValues = [
  [&quot;C&quot;, &quot;+-&quot;, &quot;%&quot;, &quot;/&quot;],
  [7, 8, 9, &quot;X&quot;],
  [4, 5, 6, &quot;-&quot;],
  [1, 2, 3, &quot;+&quot;],
  [0, &quot;.&quot;, &quot;=&quot;],
];

const App = () =&gt; {
  return (
    &lt;Wrapper&gt;
      &lt;Screen value=&quot;0&quot; /&gt;
      &lt;ButtonBox&gt;
        {
          btnValues.flat().map((btn, i) =&gt; {
            return (
              &lt;Button
                key={i}
                className={btn === &quot;=&quot; ? &quot;equals&quot; : &quot;&quot;}
                value={btn}
                onClick={() =&gt; {
                  console.log(`${btn} clicked!`);
                }}
              /&gt;
            );
          })
        }
      &lt;/ButtonBox&gt;
    &lt;/Wrapper&gt;
  );
};

export default App;

Then this is the index.js:

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;

import &#39;./index.css&#39;;
import App from &#39;./App&#39;;

ReactDOM.render(
  &lt;React.StrictMode&gt;
    &lt;App /&gt;
  &lt;/React.StrictMode&gt;,
  document.getElementById(&quot;root&quot;)
);

答案1

得分: 1

尝试像这样指定根元素:

import React from 'react';
import ReactDOM from 'react-dom';
import { createRoot } from 'react-dom/client';

import './index.css';
import App from './App';

const root = createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
英文:

Try to specify the root element like this:

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom&#39;;
import { createRoot } from &#39;react-dom/client&#39;;

import &#39;./index.css&#39;;
import App from &#39;./App&#39;;

const root = createRoot(document.getElementById(&#39;root&#39;));
root.render(
  &lt;React.StrictMode&gt;
    &lt;App /&gt;
  &lt;/React.StrictMode&gt;
);

答案2

得分: 1

从控制台中的错误消息来看,似乎在你的 index.js 文件中的导入语句存在问题。错误明确指出,ReactDOM 是从 'react-dom/client' 导入的,这不是一个有效的导入路径。

为了解决这个问题,你应该更新你的 index.js 文件中的导入语句,将导入路径改为从 'react-dom' 导入 ReactDOM,而不是 'react-dom/client'。以下是修正后的导入语句:

import ReactDOM from 'react-dom';
英文:

From the error message in the console, it appears that there is an issue with the import statement in your index.js file. The error specifically states that ReactDOM is imported from 'react-dom/client', which is not a valid import path.

To fix this issue, you should update the import statement in your index.js file to import ReactDOM from 'react-dom' instead of 'react-dom/client'. Here's the corrected import statement:

import ReactDOM from &#39;react-dom&#39;;

答案3

得分: 1

import React from 'react';
import { createRoot } from 'react-dom/client';

import './index.css';
import App from './App';

// 渲染你的 React 组件而不是
const root = createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

参考链接: https://react.dev/learn/add-react-to-an-existing-project#step-1-set-up-a-modular-javascript-environment

英文:

You can follow intructions below

import React from &#39;react&#39;;
import { createRoot } from &#39;react-dom/client&#39;;

import &#39;./index.css&#39;;
import App from &#39;./App&#39;;

// Render your React component instead
const root = createRoot(document.getElementById(&#39;root&#39;));
root.render(
  &lt;React.StrictMode&gt;
    &lt;App /&gt;
  &lt;/React.StrictMode&gt;
);

refs: https://react.dev/learn/add-react-to-an-existing-project#step-1-set-up-a-modular-javascript-environment

huangapple
  • 本文由 发表于 2023年6月22日 18:44:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定