React folder structure and what is props and state
3 min readDec 2, 2023
The folder structure of a React project can vary based on project size, complexity, and personal/team preferences. However, there are some common patterns and best practices that many developers follow. Here’s a typical React folder structure:
my-react-app/
|-- node_modules/
|-- public/
| |-- index.html
|-- src/
| |-- assets/
| |-- components/
| | |-- Component1/
| | | |-- Component1.js
| | | |-- Component1.css
| | |-- Component2/
| | |-- Component2.js
| | |-- Component2.css
| |-- pages/
| | |-- Home/
| | | |-- Home.js
| | | |-- Home.css
| | |-- About/
| | |-- About.js
| | |-- About.css
| |-- services/
| | |-- api.js
| |-- utils/
| | |-- helperFunctions.js
| |-- App.js
| |-- index.js
| |-- index.css
|-- .gitignore
|-- package.json
|-- README.md
Let’s break down the structure:
node_modules/
: The folder where project dependencies are installed. It's typically generated and managed by npm or yarn.public/
: This directory contains static assets and the HTML file where your React app is mounted.index.html
: The main HTML file where the React app is injected.src/
: This is the heart of your application and where most of your code resides.assets/
: Static files like images, fonts, or other assets.components/
: Reusable UI components. Each component may have its folder containing the component file (e.g.,Component1.js
) and associated styles (e.g.,Component1.css
).pages/
: Higher-level components that represent different pages in your application. Each page may have its folder with the page component file and styles.services/
: Utility functions or services, such as API calls.utils/
: General utility functions or helper functions.App.js
: The main component that serves as the entry point for your React app.index.js
: The file responsible for rendering the React app into the HTML document.index.css
: Global styles for the entire application..gitignore
: A file that specifies which files and directories should be ignored by version control (e.g., Git).package.json
: Contains metadata about the project and its dependencies.README.md
: Documentation for your project.
Props and State in React:
Props:
- Props (short for “properties”) are a way of passing data from a parent component to a child component in React. They are similar to function arguments in traditional programming.
- Props are immutable, meaning that they cannot be modified by the child component. The child component receives data from the parent component through props and uses it to render its UI.
// ParentComponent.js
import ChildComponent from './ChildComponent';
function ParentComponent() {
const data = "Hello from parent!";
return <ChildComponent message={data} />;
}
// ChildComponent.js
function ChildComponent(props) {
return <p>{props.message}</p>;
}
State:
- State is a way for a component to maintain and manage its own data that can change over time. Unlike props, state is mutable and can be updated using the
setState
method. - State is primarily used for handling user input, triggering re-renders, and managing the internal state of a component.
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
In this example, the useState
hook is used to create a state variable count
, and the setCount
function is used to update its value. The current count is displayed in the UI, and clicking the "Increment" button updates the count.