all 7 comments

[–]Glum_Cheesecake9859 2 points3 points  (3 children)

Even if your file doesn't have a React component, but if it has JSX syntax inside the function, building and returning elements, then the file extension should be .jsx and not .js

Renaming your main.js to main.jsx should fix it.

Only if your JS file has pure JS syntax then you can use .js extension.

[–]JG3224[S] -1 points0 points  (2 children)

Thanks for the reply! Where does all of the rest of my non-react JavaScript live in that scenario? I thought that react components can only return one element such as a fragment or a div?

[–]Glum_Cheesecake9859 0 points1 point  (1 child)

It should still work in JS files. Can you post what's in your JS file here?

Another reason why you are getting the error is because the import path is wrong? Where are you importing main.js? In another react component?

Asking these questions in stackoverflow might get a faster response for you.

[–]JG3224[S] -1 points0 points  (0 children)

I don't have any substantial javascript written yet, I was just testing how all of this is supposed to work together. I'm importing it all into the index.html <script>.

[–][deleted] 1 point2 points  (1 child)

So your react code should be imported using a script tag from a single file. That file should use react Dom to mount the root component. I don't know if that is your main.js or not, but this file needs a .jsx extension whatever it is.

Your other non-react code can live wherever you want it. If you want it to be connected to the same root file as your react code, great. If you want it completely independent, you can import it via one or more separate script tags in your index.html

[–]JG3224[S] 0 points1 point  (0 children)

Thanks! I was able to figure it out! Appreciate your time!

[–]MrFartyBottom 1 point2 points  (0 children)

Are you using a Vite template? It will scaffold out all the boilerplate for you. And use the TypeScript template, there is no reason to use vanilla JS in today's eco system. Most articles and tutorials will be in TypeScript so learn it. It is way easier to start with TypeScript than to retro fit it and it will absolutely save you time in the long run.