简体   繁体   English

React.js 应用程序在开发模式下运行良好,但在构建(生产模式)后出现错误

[英]React.js app works fine in development mode but has errors after building (production mode)

The error错误

Well, it's have been hours I'm trying to find out why I got this error (only in when running build):好吧,我已经花了几个小时试图找出为什么会出现此错误(仅在运行构建时):

错误

The architecture架构

Current directory is MySuperProject/src/Components/
Other non usefull files and directories are omitted.
.
├── Rentals                             
│   ├── modals
│   │   └── AddRentalModal.js            Not usefull here
│   ├── RentalListItem.js                https://pastebin.com/QSBncsHw
│   └── RentalsList.js                   https://pastebin.com/QufkCm85
├── Table
│   ├── Table.js                         https://pastebin.com/sq1jBPg1
└── Utils
    └── axios.util.js                    Not usefull here, axios setting

Environment环境

  • NodeJS: 15.2.0节点JS:15.2.0
  • ReactJS: 16.13.1 ReactJS:16.13.1
  • Yarn: 1.22.5纱线:1.22.5

What I do我所做的

In my main file, RentalsList.js, I:在我的主文件 RentalsList.js 中,我:

  1. Get some data on componentDidMount() from /rentals on my API从我的 API 上的 /rentals 获取一些关于 componentDidMount() 的数据
  2. Update state with the API's answer使用 API 的答案更新 state
  3. Display a component (../Table/Table.js) using RentalListItem (./RentalListItem.js) as item for this table.使用 RentalListItem (./RentalListItem.js) 作为此表的项目显示组件 (../Table/Table.js)。 To achieve that, I pass RentalListItem as prop in named TableItem.为此,我将 RentalListItem 作为命名 TableItem 中的道具传递。

Table component is called like that:表组件是这样调用的:

<Table
  columns={columns}            // Columns of my table
  datas={this.state.rentals}   // Data I got from my API
  TableItem={RentalListItem}   // TableItem which is imported from ./RentalListItem.js
/>

Here is an example of datas returned by API:以下是 API 返回的数据示例:

[
   {
      "name":"Some name",
      "ref":"AB01234",
      "availability_date": 1605237860,
      "cost": 123.45,
      "type": 0,
      "status": "Available",
      "_id":"5f667cb47919fda09795ccad",
      "address":{
         "number": 1,
         "way_type": "street",
         "way_name": "name",
         "postal_code": 12345,
         "city": "StackyCity",
         "_id":"5fadfc0e2add5eb934c497bb"
      },
      "added_by":{
         "firstname":"Me",
         "lastname":"Andmyself",
         "_id":"5fac8ca0b86ee219381252eb",
         "agency":{
            "ref": "CD56789",
            "name": "agency name",
            "email": "an@email.com",
            "_id":"5fac8f3db86ee219381252ec"
         }
      }
   }
]

My question我的问题

I don't have any error while running it under development mode.在开发模式下运行它时没有任何错误。 Indeed, I only have this error after building my code (for production environment)事实上,我只有在构建我的代码后才会出现这个错误(用于生产环境)

Why do I get this error in production mode?为什么在生产模式下会出现此错误?

Research tracks研究方向

  • I noticed that after yarn build , my componenent RentalListItem is rendered as a function t(e) but when I try to call it as a function it tells me that's a class. Well:)我注意到在yarn build之后,我的组件 RentalListItem 呈现为 function t(e) 但是当我尝试将其称为 function 时,它告诉我这是一个 class。嗯:)

  • When calling directly RentalListItem as a component on RentalsList I have no error当直接调用 RentalListItem 作为 RentalsList 上的组件时,我没有错误

  • In Table.js, this.props.TableItem seems to be well defined when it's called在 Table.js 中,this.props.TableItem 在调用时似乎定义明确

I suspect it's related to this JSX:我怀疑它与这个 JSX 有关:

<this.props.TableItem index={index} ... />

Try writing it like this:尝试这样写:

const TableItem = this.props.TableItem

...

<TableItem index={index} ... />

I believe your code is valid JSX, but it's not common to use dot notation like that, and my theory is that some part of the build process transforms it incorrectly.我相信您的代码是有效的 JSX,但像那样使用点符号并不常见,我的理论是构建过程的某些部分错误地转换了它。 In order to understand exactly what is causing this, we'd need to know what your build setup looks like.为了准确了解是什么导致了这种情况,我们需要知道您的构建设置是什么样的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular / JS Express应用程序在生产模式下处于无限路由循环中,在开发模式下运行良好 - Angular/JS Express app in infinite routing loop in production mode, works fine in development mode 为什么生产模式(React.js)会出现#130 错误? - Why does an error #130 appear in production mode (React.js)? 在生产中出现此错误,但在开发中工作正常 - StripeInvalidRequestError: The `line_items` parameter is required in payment mode - Getting this error in production but works fine in development - StripeInvalidRequestError: The `line_items` parameter is required in payment mode 在webpack生产模式下发生错误,但在开发模式下运行良好 - An error happens in webpack production mode, but but works well at development mode Rails 4 App-JS在开发模式下可与Webrick一起使用,但不适用于Passenger / Apache2 - Rails 4 App - JS works with Webrick but not Passenger/Apache2 in Development Mode 生产模式下的UglifyJSPlugin显示一个React开发模式警告 - UglifyJSPlugin in production mode shows a React development mode warning React 应用程序无法通过其请求在生产中访问我的快速应用程序,但在开发中运行良好 - React app can't reach my express app in production with its requests but works fine in development Vue.js Vite 应用程序在开发中工作,但在构建后崩溃 - Vue.js Vite app works in development but crashes after building JavaScript开发和生产模式 - JavaScript development and production mode 内置的 React 应用程序工作正常,但开发模式下的相同应用程序(npm start)工作不正常 - Built React App works fine but same App in Dev mode (npm start) works not properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM