I have to add some functionality to a small project and it’s a pretty weird project I have to mention. As soon as I bring useState to any component, the component breaks. Even a simple one:
import React, { useState } from 'react';
export function Example() {
const [count] = useState(0);
return ( <div>{count}</div> );
}
gives an error:
Uncaught ReferenceError: exports is not defined
telling that some other component can’t import and use Example component.
I can normally use other hooks (I actually use useEffect in some of components), but whenever I use useState I immediatelly get the error mentioned above. Since the project is very small, only a few components, there were no useStates before I started woking on it. Maybe there’s somthing wrong with webpack config, I don’t know.
Please help me fix this strange bug!
2
Answers
This is a fix for your code. Adding default to the export.
or
If you intend to export the component as such:
you will have to import it this way:
Please try these three steps:
webpac.config.js file.
Run that as Terminal.
And as .babelrc file.