File Structure in React
Understand the react flow and file structure

👋 Hi, I'm Mohd Ahsan Raza Khan! I know you might be thinking my name is quite long, right? Don't worry, you can call me MARK. I'm a software developer with a passion for building web applications and sharing knowledge through writing. I love exploring new technologies and frameworks, and I'm particularly interested in JavaScript, ReactJS, NodeJS, ExpressJS, and MongoDB. In my free time, I enjoy learning about new technologies and writing tutorials to help others learn and grow in their coding journey. I'm always excited to connect with fellow developers, so feel free to reach out to me on LinkedIn. Let's build something amazing together!
Deep dive into the basic react file structure
node_modules: stores all React dependencies and packages, including core React modules (e.g.,react,react-dom) and their associated dependencies (e.g.,webpack,babel,ESLint), essential for building and running the React application.package-lock.json: it is automatically generated by npm when packages are installed, ensuring a stable version of dependencies is locked.gitignore: a text file that instructs Git on which files or folders to ignore within a project.publicfolder:The crucial file is
index.html. You only need to understand two main things:<noscript>tag: which is used to provide alternative content or instructions that will be displayed when JavaScript is disabled or not supported in the user's browser.An empty
divwith anidis"root"
srcfolder:Most of the time we work on the
srcfolder. Let's break down the files one by one.index.jsfile:react: is a core fundamental library (which takes all the references)react-dom: is the implementation for the web.ReactDOM: creates a virtual DOM.createRoot: expect to get an element from the.htmlfile.render: run/execute.<React.StrictMode>: is a wrapper component in React that enables additional development checks and warnings to catch potential issues early during development, including identifying unsafe lifecycles, deprecated APIs, unexpected side effects, and legacy context usage.<App />: is a custom tag. (JSX)
app.js: is a function that returns HTML code. Export the function.react-scripts: is a package that loads a javascript file inside index.html.
React File Structure in Vite
scriptfile: is directly loaded inside theindex.htmlwithout installing areact-scriptpackage.
Creating another component in Vite
Rules:
The file convention should be
.jsx.The component name should be Capitalized.
Use fragments (denoted as
<> </>) to wrap the JSX elements.
Good Practice: The file name should also be Capitalized.
Creating another component in basic react
Rules:
File convention should be .jsx if it returns HTML code, while .js if there is only js code. But both conventions are working.
The component name should be Capitalized.
Components are simply a javascript function.
Example of creating component
File Name: Chai.jsx
function Chai() {
const username = "Mark"
return (
<h2>Chai is ready | {username}</h2>
)
}
export default Chai
File Name: App.jsx
import Chai from './Chai';
function App() {
return (
<>
<h1>Chai aur React with Vite | Mark </h1>
<Chai />
</>
)
}
export default App
File Name: main.jsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
ReactDOM.createRoot(document.getElementById('root')).render(
<App />
)
Add-on:
Crtl+Shift+p: to reload page.




