I've recently started a react project and I found some util commands with npm to generate some projects automatically. No problem with that, but the project structure was a bit confusing for me so I've searched for some explanations on google, everything was fine, and I've found something interesting on https://www.pluralsight.com/guides/file-structure-react-applications-created-create-react-app:
While components can reside in src/components/my-component-name, it is recommended to have an index.js inside that directory. Thus, whenever someone imports the component using src/components/my-component-name, instead of importing the directory, this would actually import the index.js file.
This index.js file, how should I create it to return the component?
For example I want to create some pages for different routes:
import React, {Component} from 'react'
class MyPage extends Component{
render() {
return (
<div>
My Page
</div>
);
}
}
export default MyPage;
Now, the index.js should look like this?
import React from "react";
import MyPage from "src/pages/MyPage";
const mypage = () => {
return <MyPage/>
};
export default mypage;
And the project structure:
src
|___pages
|____MyPage.jsx
|____index.js
And there should be no problem with the Router component because all I have to do without index.js is to import MyPage
and do something like this:
<Router>
<div>
<Route path="/" component={MyPage} />
</div>
</Router>
Is this the correct way to create that index.js file?