ReactJS - 未捕获的 TypeError:无法设置仅具有 getter 的 #<Object> 的属性默认值
2022-07-03
2014
使用 React.js 编码时遇到错误。我对此没有任何想法。因此我不知道如何提出这个问题。此外,我附上了一个错误文件的图片。放上 App.js、Header.js 和 package.json 文件。我尝试解决这个问题一个小时了,但没有成功。请帮忙。
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"
]
}
}
2个回答
尝试将
img
容器的
className
更改为:
<div className={classes['main-image']}>
lpizzinidev
2022-07-03
您的 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']}> {/* 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
Bhavesh Daswani
2022-07-03