英文:
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
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/client';
import './index.css';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
答案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 '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>
);
答案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 'react-dom';
答案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>
);
英文:
You can follow intructions below
import React from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App';
// Render your React component instead
const root = createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论