Hey friends! π
If you're diving into React or already building cool stuff with it, this blog is for you. I'm putting together a series I call React Bits β small, digestible pieces of React knowledge that helped me write cleaner, smarter, and more maintainable React code.
In this post, I'll walk you through:
Setting up a React app the modern way
Best folder structures
Core concepts (like useState, useEffect, and props)
Smart practices I wish I knew earlier
Letβs go bit by bit. π
π οΈ Setting Up React β Quick and Clean There are multiple ways to get started with React, but my go-to (and the one I recommend) is Vite. Itβs blazing fast and much simpler than older setups like Create React App (CRA).
β Option 1: React with Vite (Recommended)
npm create vite@latest react-bits -- --template react
cd react-bits
npm install
npm run dev
Your app is live on localhost:5173.
π Option 2: React with CRA (If you're already familiar)
npx create-react-app react-bits
cd react-bits
npm start
π§± Folder Structure That Actually Makes Sense Hereβs a simple folder layout I use to keep things clean:
react-bits/
βββ src/
β βββ components/ # Reusable UI bits
β βββ pages/ # Route-based views
β βββ hooks/ # Custom React hooks
β βββ App.jsx
β βββ main.jsx
βββ public/
βββ package.json
As your project grows, this keeps your code organized without going full "enterprise mode."
π€ JSX β Looks Like HTML, Feels Like JS JSX lets us write HTML in our JavaScript. It's what makes React super intuitive.
const Welcome = () => {
return <h1>Hello, React Bits!</h1>;
};π‘ Tip: JSX always needs one parent element. Use <> </> fragments if needed.
βοΈ Core React Concepts Youβll Use Daily π useState: For Keeping Track of Values
const [count, setCount] = useState(0);
π§ useEffect: For Side Effects (like fetching data)
useEffect(() => {
console.log("Component mounted");
return () => console.log("Component unmounted");
}, []);
π¦ Props: Pass Data Between Components
const Greet = ({ name }) => <h2>Hello, {name}!</h2>;
π§© Composing Components
function App() {
return <Greet name="React Bits Reader" />;
}π‘ React Bits β Practices That Help a Lot Here are some βbitsβ that really improved my React code over time:
const Card = ({ title, description }) => (
<div>
<h2>{title}</h2>
<p>{description}</p>
</div>
);
<button onClick={() => handleClick(id)}>Click</button> // βUse this:
const handleClickId = (id) => () => handleClick(id);
<button onClick={handleClickId(id)}>Click</button> // β
useEffect(() => {
const interval = setInterval(() => console.log("tick"), 1000);
return () => clearInterval(interval); // π Cleanup
}, []);
items.map((item) => <ListItem key={item.id} data={item} />);Using indexes as keys can cause UI bugs, especially when items change order.
function useToggle(initial = false) {
const [value, setValue] = useState(initial);
const toggle = () => setValue((v) => !v);
return [value, toggle];
}π― Why These Bits Matter These small improvements β like organizing files better, avoiding anonymous functions, or writing custom hooks β might seem minor, but over time they make your codebase more scalable, testable, and fun to work with.
That's what React Bits is all about.