简体   繁体   English

缩小的 React 错误 #152 - 如何解决这个问题?

[英]Minified React error #152 - how to resolve this?

I am getting below error even though I am building using dev environment.即使我正在使用开发环境进行构建,我也遇到了错误。 Below is my package.json file.下面是我的 package.json 文件。

Error: "for the full message or use the non-minified dev environment for full errors and additional helpful warnings."错误:“获取完整消息或使用未缩小的开发环境获取完整错误和其他有用的警告。”

Package.json file: Package.json文件:

 "scripts": {
    "compile": "webpack",
    "start": "node server.js",
    "dev": "webpack --mode development",
    "build": "webpack --mode production",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

I do npm run build and then npm run start.我做 npm 运行构建,然后 npm 运行开始。 Still I am getting the same minified error, I tried to delete bundle.js or./dist folder and done building in dev environment, still I face the same issue.我仍然收到相同的缩小错误,我尝试删除 bundle.js 或 ./dist 文件夹并在开发环境中完成构建,但我仍然面临同样的问题。

Could anyone help me, how can I avoid this minified version and debug the files easily in local?谁能帮助我,我怎样才能避免这个缩小版本并在本地轻松调试文件?

Remove any comment inside render / return method删除render / return方法中的任何注释

I used this regex in VSCode to find the troublesome comments: (\\s*\\n?\\s*//我在 VSCode 中使用了这个正则表达式来查找麻烦的注释: (\\s*\\n?\\s*//

在此处输入图片说明

More help here更多帮助在这里

The error description is here , you return nothing as result of render method of you react component.错误描述在这里,你的反应组件的render方法没有返回任何结果。 Return something else instead of it.返回其他东西而不是它。

In case it helps someone, I resolved this problem by returning null instead of false (my function is wrapped inside an HOC)如果它对某人有帮助,我通过返回 null 而不是 false 解决了这个问题(我的函数被包装在 HOC 中)

Edit: sometimes I did not want to render anything.编辑:有时我不想渲染任何东西。 In such case, I returned false in the render method as something like this:在这种情况下,我在 render 方法中返回 false ,如下所示:

    render(){
        return condition && <ThingsIWantToDisplay/>; //condition is a boolean
    }

The component class is then wrapped within an HOC (to provide React Context) like this:然后将组件类包装在 HOC(以提供 React Context)中,如下所示:

export default HOC(Component);

When condition is false , an error occurs in the production build.conditionfalse时,生产构建中会发生错误。

Changing the render function as shown below alleviated the problem:如下所示更改渲染功能缓解了问题:

    render(){
        return condition?<ThingsIWantToDisplay/>:null; //condition is a boolean
    }

I have the same issue check your useState import我有同样的问题检查你的 useState 导入

import { useState } from 'react/cjs/react.production.min'; change into import react,{usestate} from 'react'更改为import react,{usestate} from 'react'

I had same issue.我有同样的问题。 So i removed comments inside my render function and it worked!所以我删除了渲染函数中的注释并且它起作用了!

I accidentally imported it from here:我不小心从这里导入了它:

import {useState} from "react/cjs/react.production.min";

use import from import React, {useState} from 'react';使用从import React, {useState} from 'react'; instead反而

That fixed the issue这解决了问题

In our instance, this was not related to comments inside the react HTML at all, it had to do with CMS changes.在我们的例子中,这与反应 HTML 中的评论完全无关,它与 CMS 更改有关。 If you go through your react HTML and find no comments, try taking a look at other tools you are using that may be having an effect on your site.如果您 go 通过您的反应 HTML 并没有发现任何评论,请尝试查看您正在使用的其他可能对您的网站产生影响的工具。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM