TypeError: react__WEBPACK_IMPORTED_MODULE_2___default(...) is not a function. How do I solve this? TypeError: react__WEBPACK_IMPORTED_MODULE_2___default(...) is not a function. How do I solve this? reactjs reactjs

TypeError: react__WEBPACK_IMPORTED_MODULE_2___default(...) is not a function. How do I solve this?


I am just going to point out you are importing from "react" lib twice

// You have: (look closely at libs you are importing from and how)import React from 'react';import LoginForm from './loginForm'import useState from "react"// Should be:import React, { useState } from 'react';import LoginForm from './loginForm'// Why?// Because useState is one of React libs' export default's apis / methods// aka useState is just a part of the React default export but also is itself, an export// React.useState() is how it would look if you just imported the React lib itself and nothing else// how I personally handle any react apis is via ==> import React, { useState } from 'react// apart from loving seeing all libraries and individual apis imported // as soon as I see a file to get a sense of its potential functionalities, // read my detailed explanation below

Here, you are literally importing (export default react) from the entire React lib and simply naming it a random string useState and then trying to use that how you use the real React.useState() api/method.

So you trying to use useState like the actual React useState api in this manner will absolutely cause an error because you are basically saying this require('react')() when you import the default import of react lib versus an api that is part of that default export, or it is itself an export in which you need to deconstruct from the lib in the import statement, not sure related but you 100% have malformed code I cannot believe no one even mentioned this?

For further example, for it to work as you have it (although eslint should be screaming about duplicate imports by now before you even hit save) you would have to do useState.useState(), which clearly is not what you want. Some people don't mind React.useState() but I personally will shun you haha jk, but destruct from import!!! please (:

Using Best Coding Standards is key to being a great software engineer on a team and even in your own personal projects for all of the reasons, and will absolutely increase your DX and output overall.

Hope this helped my dude. We all have gone through these little quirks that once you learn, add that to your "things I know for sure" list and keep trucking


Please add more information. Like what was the API/module you choose to import and is it using

export default function

vs

export function

How you are consuming it?

You might have importing it as

import Something from './something';

Whereas, your something.js might look like:

export function Something(){} 


If you updated webpack to version 5.x.x, then this might be related to output.library setting issue

so if you have library setting in webpack output try to remove it:

output: {  path: path.resolve(__dirname, 'build'),  filename: 'painterro.commonjs2.js',  library: 'Painterro',  // ⏪ this is the issue  libraryTarget: 'commonjs2', },