How do I get my App.js file to work properly?
P粉063039990
P粉063039990 2023-09-11 21:07:58
0
2
565

I'm new to coding so I'm following this tutorial: https://www.sitepoint.com/react-tutorial-build-calculator-app/ I can't render any elements. This is what the console displays when running a web application

This is my App.js code:

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 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")
);

P粉063039990
P粉063039990

reply all(2)
P粉426906369

Try specifying 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>
);
P粉138871485

Judging from the error message in the console, there seems to be a problem with the import statement in the index.js file. The error specifically states that ReactDOM is imported from "react-dom/client", which is not a valid import path.

To resolve this issue, you should update the import statements in the index.js file to import ReactDOM from "react-dom" instead of "react-dom/client". Here is the corrected import statement:

import ReactDOM from 'react-dom';
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!