All I'm trying to do is split my React components into separate JSX files and be able to reference one from the other. I'm using ReactJS.Net in a .NET Core 3.1 MVC project. Chrome is up to date (v80). I'm quite new to JS and React so assume there's something basic I don't know.
When I place the two classes within the same file, it works as expected. ESLint reasonably suggests that I split components to separate files. When I break them into two as shown with import
and export
, I get the following error when I try to run it:
Uncaught SyntaxError: Cannot use import statement outside a module
The error is on the import
line at the top of test.jsx. I'm assume my lack of understanding of where import goes and what constitutes a module is not helping.
Test.jsx looks like:
import SimpleItem from './js/SimpleItem';
// tried variations on: <script type="module" src="./js/SimpleItem.jsx"></script>;
class SimpleContainer extends React.Component {
render() {
return (
<div className="simpleContainer">Word<SimpleItem /></div>
);
}
}
ReactDOM.render(<SimpleContainer />, document.getElementById('content'));
SimpleItem.jsx looks like:
class SimpleItem extends React.Component {
render() {
return (
<div className="simpleItem">I'm Simple!</div>
);
}
}
export default SimpleItem;
I've spent a maddening amount of time Googling, but either posts I've found don't seem to solve the problem or my ignorance isn't understanding what seems like it should be a dreadfully straightforward proposition in most any language since last century.
As a couple examples, ReactJS inside .NET framework project - Cannot use import statement outside a module asks a similar question giving same error. I'm attempting to split my components vs. importing React (probably the same principle though). Suggestions led me to ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import. But that didn't address it.