解析错误:标识符“App”已经宣布了

发布于 2025-01-10 10:58:14 字数 1486 浏览 0 评论 0原文

我是 React.js 的初学者;我正在构建一个简单的音乐播放器应用程序,但在 App.js 文件中它给了我一个错误。下面我分享App.js文件的代码和浏览器生成的错误。

import React, { Component } from 'react'
import './App.css';

import PlayList from "./componentes/PlayList"
import SearchBar from "./componentes/SearchBar"
import SearchResults from "./componentes/SearchResults"


class App extends React.Component{
//code to be written
}

function App() {
  return (
    <div>
      <h1>
        <a href="http://localhost:3000">Melophile</a>
      </h1>
      <div className="App">
        <SearchBar onSearch={this.search}/>
        <div className="App-playlist">
          <SearchResults searchResults={this.state.searchResults} onAdd={this.doThese}/>
          <PlayList playlistTracks={this.state.playlistTracks} onNameChange={this.updatePlaylistName} onRemove={this.removeTrack} onSave={this.savePlaylist} />
          
        </div>
      </div>
    </div>
  );
}

export default App;

浏览器错误:

Compiled with problems:

ERROR in ./src/App.js

Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:\Users\AFRIDI\OneDrive\Desktop\STUDY NOTES\SEM - 4\Minor Project - 1\melophile-app\src\App.js: Identifier 'App' has already been declared. (13:9)

  11 | }
  12 |
> 13 | function App() {
     |          ^
  14 |   return (
  15 |     <div>
  16 |       <h1>

寻求高手帮忙解决这个问题。

I'm a beginner in React.js; I was building a simple Music player app, but in App.js file it gave me an error. Below I am sharing the code of App.js file and the browser generated error.

import React, { Component } from 'react'
import './App.css';

import PlayList from "./componentes/PlayList"
import SearchBar from "./componentes/SearchBar"
import SearchResults from "./componentes/SearchResults"


class App extends React.Component{
//code to be written
}

function App() {
  return (
    <div>
      <h1>
        <a href="http://localhost:3000">Melophile</a>
      </h1>
      <div className="App">
        <SearchBar onSearch={this.search}/>
        <div className="App-playlist">
          <SearchResults searchResults={this.state.searchResults} onAdd={this.doThese}/>
          <PlayList playlistTracks={this.state.playlistTracks} onNameChange={this.updatePlaylistName} onRemove={this.removeTrack} onSave={this.savePlaylist} />
          
        </div>
      </div>
    </div>
  );
}

export default App;

Browser error:

Compiled with problems:

ERROR in ./src/App.js

Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:\Users\AFRIDI\OneDrive\Desktop\STUDY NOTES\SEM - 4\Minor Project - 1\melophile-app\src\App.js: Identifier 'App' has already been declared. (13:9)

  11 | }
  12 |
> 13 | function App() {
     |          ^
  14 |   return (
  15 |     <div>
  16 |       <h1>

Looking for experts help to solve this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叫嚣ゝ 2025-01-17 10:58:14

您看到的错误是因为您有以下两件事:

class App extends React.Component{

因此

function App() {

,在识别您所指的内容时存在问题。您要么需要重命名这些位置之一,要么决定要使用的内容(类或函数)。

The error you are seeing is because you have these two things:

class App extends React.Component{

And

function App() {

Therefore, there is a problem identifying what you are referring to. You either need to rename one of these places or decide what you want to work with (class or functions).

◇流星雨 2025-01-17 10:58:14

您有一个名为 App 的类和一个名为 App 的函数。不能让两个东西同名。

看来您所做的是将基于类的 React 语法与更现代的函数语法混合在一起。由于您还没有在类中编写任何内容,因此可以安全地假设您实际上并未使用基于类的语法,因此您可以简单地删除该类(删除以下内容)

class App extends React.Component{
//code to be written
}

You have a class named App and a function named App. You can't have two things named the same.

What it appears you've done is mixed class-based React syntax with more modern functional syntax. Since you don't have anything written inside the class yet, it seems safe to assume you're not actually using class-based syntax, so you can simply remove the class (delete the following)

class App extends React.Component{
//code to be written
}
桃扇骨 2025-01-17 10:58:14

在您的代码中,您声明了 App 类,然后声明了 App 函数。因此,该应用程序已被声明。

class App extends React.Component{
    //code to be written
}
        
function App() {

所以你可以这样做,

  1. 重命名任何一个
  2. 删除任何一个
  3. 使用类或函数的差异名称在类内声明函数

In your code you declared App class and then declare App function. So It's giving that App is already declared.

class App extends React.Component{
    //code to be written
}
        
function App() {

So you can do,

  1. Rename any one
  2. Remove any one
  3. Declare function inside class with diff name of class or function
梦萦几度 2025-01-17 10:58:14

您可以使用基于类的组件或基于钩子的组件。你不能混合使用它们。
您可以查找更多详细信息.. https://reactjs.org/docs/hooks-intro .html

you can use class based component or hook based component. you can't use mix of them.
you can look for more detail information.. https://reactjs.org/docs/hooks-intro.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文