Tag Archives: react

How to Solve React devtools plug-in Error

The developer debugging tool plug-in of react will prompt an error in chrome after installation:

react-refresh-runtime.development.js:465

Solution:

Under the react project (not the plug-in file directory), find \node_Modules\@pmmmwh\react refresh webpack plugin\client

Reactrrefreshentry.js file, comment out this line:

//RefreshRuntime.injectIntoGlobalHook(safeThis);

[Solved] IE Browser Error: unhandled promise rejection error: access is denied. File stream Download

There are many export files and download files in the project. The front end adopts the file stream download method, requests the back-end interface and returns the file stream
at first, it was not clear that the ordinary dynamic creation a tag method was not compatible with ie. later, the bug “unhandled promise rejection error: access denied” appeared in the test on ie. after seeing this problem, it was directly copied to Baidu, and then the result was that the promise method might be wrong. After changing all the promise methods, an error was reported, Later, I thought about whether the way to create a tag was not compatible with ie, and Baidu adopted the method of downloading file stream compatible with ie. sure enough, I got the following solution and recorded it.

IE browser has Microsoft’s own mssaveblob method. The download of a tag is not compatible with ie.

//Since the method is used in several places in the page, it is wrapped as a component
// Don't forget to set the responseType='blob' in the request header, the react project is already encapsulated in the request
export const exportSaveData = (data, filename) => {
    let blob = new Blob([data], { type: 'application/x-www-form-urlencoded' });
    let blobUrl = window.URL.createObjectURL(blob);
    if (window.navigator.msSaveBlob) { // determine if the method is available for Internet Explorer
        try {
            window.navigator.msSaveBlob(blob, filename)
        } catch (e) {
            console.log(e);
        }
    } else {
        // Google Chrome Create a tag Add download attribute to download
        const aElement = document.createElement('a'); // create a tag dynamically
        document.body.appendChild(aElement); // append the created a tag to the body
        aElement.style.display = 'none'; //you can set the a tag not to be visible
        aElement.href = blobUrl; a tag's href is the processed blob
        aElement.download = filename;
        aElement.click();
        document.body.removeChild(aElement);
        window.URL.revokeObjectURL(blobUrl); //release the blob object
    }
}

Perfectly solve the problem that IE cannot export

When react devtools is enabled, an error is reported for the project

I installed the latest version of react devtools on the V3 branch. I installed it according to a series of operations in the official documents, and then ran the project to report an error:

However, the previous project was running normally, so I searched the Internet and found that most of them said that the plug-in react devtools was disabled directly, and there was no language. Why do I install it??Too much memory??

So I looked at the error message again, and then commented out the code used in my project according to the following error message

according to its path, and then re yarn start, there was no error message

I read it online and said that this is the reason for the version. I said that the previous version did not have this problem
just sauce, bye!

How to Solve DVA switches history to browserhistory Error

report errors:

Module not found: Can't resolve 'history/createBrowserHistory'

Solution:

// import createHistory from 'history/createBrowserHistory';
// Error: Cannot find module 'history/createBrowserHistory';

// Change to
import { createBrowserHistory  as createHistory} from 'history';

const app = dva({
  history: createHistory(),
});

An error is reported when using react app rewired to start the react project

An error is reported when using react app rewired to start the react project

The error contents are as follows:

 

The “injectbabelplugin” helper has been deprecated as of v2.0

The new version of react app rewired removes the injectbabelplugin, and these methods are moved to a new package called ‘Customize CRA’

Solution:
reduce react app rewired to version 2.0

npm uninstall react-app-rewired

Delete the original react app rewired with the above statement

npm i [email protected]

Then reinstall the lower version of react app rewired

Front end project construction error unexpected character ‘@’ solution

After I imported the CSS file of bootstrap, the following error messages appeared in the construction project:

ERROR in ./src/util/bootstrap/bootstrap.min.css
Module parse failed: D:\eclipseWorkspace\mbos\mbos-portal\webContent\src\util\bootstrap\bootstrap.min.css Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '@' (1:0)
    at Parser.pp$4.raise (D:\eclipseWorkspace\mbos\mbos-portal\webContent\node_modules\webpack\node_modules\acorn\dist\acorn.js:2221:15)
    at Parser.pp$7.getTokenFromCode (D:\eclipseWorkspace\mbos\mbos-portal\webContent\node_modules\webpack\node_modules\acorn\dist\acorn.js:2756:10)
    at Parser.pp$7.readToken (D:\eclipseWorkspace\mbos\mbos-portal\webContent\node_modules\webpack\node_modules\acorn\dist\acorn.js:2477:17)
    at Parser.pp$7.nextToken (D:\eclipseWorkspace\mbos\mbos-portal\webContent\node_modules\webpack\node_modules\acorn\dist\acorn.js:2468:15)

The reason is that the CSS file contains the @ symbol, and the configuration file does not specify a loader for the CSS file,

Solution: specify loader for CSS file:


{ 
	test: /\.css$/,
	loader: 'style-loader!css-loader' 
},

(a)’21442;’32771;65306;
styles.css Unexpected character’

React error boundary (What You Should Know & How to Solve)

  1. What is the error boundary?

Error boundary is a kind of react component. This component can capture JavaScript errors anywhere in its sub component tree, print these errors, and display the degraded UI , without rendering those sub-component trees that crash. Error boundaries catch errors during rendering, in the lifecycle method, and in the constructor of the entire component tree.

Note: the error boundary cannot capture the errors generated in the following scenarios

    event handling asynchronous code (such as setTimeout or requestanimationframe callback function) the server renders the errors thrown by itself (not its sub components)

If anyone (or both) of the two lifecycle methods static getderivedstatefromerror() or componentdidcatch() is defined in a class component, it becomes an error boundary. When an error is thrown, please use static getderivedstatefromerror() to render the alternate UI and componentdidcatch() to print the error message.

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    logErrorToMyService(error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      return <h1>Something went wrong.</h1>;
    }

    return this.props.children; 
  }
}

You can then use it as a regular component:

<ErrorBoundary>
  <MyWidget />
</ErrorBoundary>

The working mode of the error boundary is similar to the catch {} of JavaScript, except that the error boundary is only for the react component. Only the class component can be an error boundary component. In most cases, you only need to declare the error boundary component once and use it throughout the application.

Note that the error boundary can only catch the errors of its subcomponents, it cannot catch its own errors. If an error boundary cannot render the error message, the error will bubble to the nearest upper error boundary, which is also similar to the working mechanism of catch {} in JavaScript.

2. React error boundary

https://github.com/bvaughn/react-error-boundary
https://www.npmjs.com/package/react-error-boundary
The above address is the error boundary wheel encapsulated by the boss, which can be used directly. Please check the relevant documents for details

Error in react build packaging test environment

#NPM run build: test or yarn build: Test

        

Dotenv cli needs to be installed before use( https://github.com/entropitor/dotenv-cli )Otherwise, the following error will be reported:

dotenv -e .env.development craco build

'dotenv' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build:test: `dotenv -e .env.development craco build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build:test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\node\node_cache\_logs\2021-09-01T02_25_42_552Z-debug.log

 

To install global dotenv cli:

Execute command:   yarn global add dotenv-cli           perhaps         npm   install -g   dotenv-cli  

Installation complete:

Execute again as shown in the figure:

Packaging complete  

Module not found:Error:Can’t resolve ‘rc-animate/lib/CSSMotionList’ in ‘……’

explain

After the react project is compiled, sometimes the following error will be reported if you enter cnpm start

Add the following code to the package.json file to specify the version

"resolutions": {
  "css-animation": "1.5.0",
  "rc-animate": "2.8.2"
},

 

Add the following configuration in dependencies

"css-animation": "1.5.0",
"rc-animate": "2.8.2",

A cross-origin error was thrown. React doesn‘t have access to the actual error object in development

A cross origin error was throw. React doesn’t have access to the actual error object in development

I ran the react project in the morning. I read the reason on the Internet and learned that there was a problem with the package, which led me to delete the package several times without solving it. Finally, delete the package and then clear the cache of the package with the instruction. The package downloaded with the yarn instruction solves this problem

Webstorm startup error load error: undefined path variables

When starting webstorm, the following error will be reported:

The reason for this problem is that the less environment is not set properly. Please see the solution:

1. Open the setting panel as follows:

In the figure above, pay special attention to serial number 4. It must be set to global.

2. Restart webstorm and open it again without this error