Home  >  Q&A  >  body text

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

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粉063039990P粉063039990404 days ago582

reply all(2)I'll reply

  • P粉426906369

    P粉4269063692023-09-12 13:58:46

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

    reply
    0
  • P粉138871485

    P粉1388714852023-09-12 12:56:39

    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';

    reply
    0
  • Cancelreply