React Setup

Javascript App development
Author

Benedict Thekkel


๐Ÿš€ 1. Quick Setup (Vite + React + TypeScript)

๐Ÿ“ฆ Installation

# Create project
npm create vite@latest my-app -- --template react-ts

# Move into project
cd my-app

# Install dependencies
npm install

# Start dev server
npm run dev

๐Ÿ—‚๏ธ 2. Project Structure Explained

my-app/
โ”œโ”€โ”€ public/               # Static assets (favicon, robots.txt)
โ”‚   โ””โ”€โ”€ vite.svg
โ”œโ”€โ”€ src/                  # All source code lives here
โ”‚   โ”œโ”€โ”€ assets/           # Images, fonts, etc.
โ”‚   โ”œโ”€โ”€ components/       # Reusable UI components
โ”‚   โ”‚   โ””โ”€โ”€ Button.tsx
โ”‚   โ”œโ”€โ”€ pages/            # Pages for routes (e.g., Home, About)
โ”‚   โ”‚   โ””โ”€โ”€ Home.tsx
โ”‚   โ”œโ”€โ”€ hooks/            # Custom React hooks
โ”‚   โ”œโ”€โ”€ styles/           # Global styles (e.g., Tailwind, CSS Modules)
โ”‚   โ”œโ”€โ”€ App.tsx           # Root component
โ”‚   โ”œโ”€โ”€ main.tsx          # Entry point (ReactDOM.render)
โ”‚   โ””โ”€โ”€ types/            # Shared TypeScript types
โ”œโ”€โ”€ .eslintrc.cjs         # Linting rules
โ”œโ”€โ”€ index.html            # HTML template
โ”œโ”€โ”€ package.json          # Project config and dependencies
โ”œโ”€โ”€ tsconfig.json         # TypeScript configuration
โ””โ”€โ”€ vite.config.ts        # Vite config

๐Ÿ’ก 3. Core Files & Concepts

main.tsx โ€” Entry Point

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './styles/global.css'; // if using global styles

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

App.tsx โ€” Root Component

import React from 'react';
import Home from './pages/Home';

function App() {
  return (
    <div className="App">
      <h1>Welcome to React</h1>
      <Home />
    </div>
  );
}

export default App;

๐Ÿงฉ 4. Component Example

components/Button.tsx

import React from 'react';

type Props = {
  label: string;
  onClick: () => void;
};

const Button: React.FC<Props> = ({ label, onClick }) => (
  <button onClick={onClick} className="btn">
    {label}
  </button>
);

export default Button;

๐ŸŒ 5. Routing Setup (with react-router-dom)

๐Ÿ“ฆ Install

npm install react-router-dom

๐Ÿ› ๏ธ Setup

main.tsx

import { BrowserRouter } from 'react-router-dom';

ReactDOM.createRoot(document.getElementById('root')!).render(
  <BrowserRouter>
    <App />
  </BrowserRouter>
);

App.tsx

import { Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';

function App() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/about" element={<About />} />
    </Routes>
  );
}

๐ŸŽฃ 6. Custom Hook Example

hooks/useCounter.ts

import { useState } from 'react';

export function useCounter(initial = 0) {
  const [count, setCount] = useState(initial);
  const increment = () => setCount((c) => c + 1);
  const decrement = () => setCount((c) => c - 1);
  return { count, increment, decrement };
}

๐Ÿงช 7. Testing Setup

๐Ÿ“ฆ Install

npm install --save-dev vitest @testing-library/react @testing-library/jest-dom

Button.test.tsx

import { render, screen } from '@testing-library/react';
import Button from './Button';

test('renders button with label', () => {
  render(<Button label="Click me" onClick={() => {}} />);
  expect(screen.getByText('Click me')).toBeInTheDocument();
});

๐Ÿงผ 8. Linting + Formatting

๐Ÿ“ฆ Install

npm install -D eslint prettier eslint-plugin-react eslint-config-prettier

.eslintrc.cjs

module.exports = {
  extends: ['react-app', 'plugin:react/recommended', 'prettier'],
};

๐ŸŽจ 9. Styling Options

Approach Tools
Utility-first CSS Tailwind CSS
Component-scoped CSS Modules / Styled-components
Global CSS Plain CSS / SCSS
# Tailwind example setup
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

๐Ÿ“ฆ 10. Helpful Commands

Task Command
Start dev server npm run dev
Build for prod npm run build
Preview build npm run preview
Run tests npx vitest or npm run test
Format code npx prettier --write .

Back to top