Home > Blockchain >  ReactJS - Uncaught TypeError: Cannot set property default of #<Object> which has only a getter
ReactJS - Uncaught TypeError: Cannot set property default of #<Object> which has only a getter

Time:07-04

Image of Error

Facing an error while coding using React.js. I have know idea about it. Hence i don't know how to ask this question. Besides, I have attached an image of an error file. Putting the App.js, Header.js and package.json files. I have been trying to resolve the issue for an hour but couldn't get the success. Please help.

App.js

import { Fragment } from "react";
import Header from "./components/Layout/Header";

function App() {
  return (
    <Fragment>
      <Header/>
    </Fragment>
  );
}
export default App;

Header.js

import React, {Fragment} from 'react'
import mealsImage from '../../assets/meals.jpg'
import classes from './Header.module.css'

const Header = (props) => {
  return (
    <Fragment>
        <header className={classes.header}>
            <h1>ReactMeals</h1>
            <button>Cart</button>
        </header>
        <div className={classes=['main-image']}>
            <img src={mealsImage} alt='A Table full of delicious meals' />
        </div>
    </Fragment>
  )
}
export default Header

Package.json

{
  "name": "react-complete-guide",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "^5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

CodePudding user response:

Try to change the className of the img container to:

<div className={classes['main-image']}>

CodePudding user response:

You have issue in Header.js file, let me provide you fix code

    import React, {Fragment} from 'react'
import mealsImage from '../../assets/meals.jpg'
import classes from './Header.module.css'

    const Header = (props) => {
      return (
        <Fragment>
            <header className={classes.header}>
                <h1>ReactMeals</h1>
                <button>Cart</button>
            </header>
            <div className={classes['main-image']}> {/* issue was here you were using classes=['main-image'], you do not need = here */}
                <img src={mealsImage} alt='A Table full of delicious meals' />
            </div>
        </Fragment>
      )
    }
    export default Header
  • Related