Skip to main content
React is the most popular JavaScript library for building user interfaces. Created by Facebook, React revolutionized frontend development with its component-based architecture and virtual DOM. This comprehensive guide shows you how to build production-ready React SPAs with Mizu as your backend.

Why React?

React has become the industry standard for frontend development: Component-Based - Build encapsulated components that manage their own state, then compose them into complex UIs. Declarative - Design simple views for each state in your application, and React efficiently updates and renders the right components when data changes. Learn Once, Write Anywhere - React doesn’t make assumptions about your tech stack, so you can develop new features without rewriting existing code. Huge Ecosystem - The largest library ecosystem in frontend development. Whatever you need, there’s probably a package for it. Strong TypeScript Support - First-class TypeScript integration for type-safe components and APIs. Industry Adoption - Used by Facebook, Instagram, Netflix, Airbnb, and thousands of other companies.

React vs Other Frameworks

Choose React when:
  • You need the largest ecosystem and community
  • TypeScript integration is important
  • Team has React experience
  • Building a complex, interactive SPA
  • Job market considerations matter
Choose something else when:
  • Bundle size is critical β†’ Use Preact or Svelte
  • Learning curve matters β†’ Try Vue
  • Need full framework β†’ Use Next.js
  • Need SSR β†’ Use Next.js

Quick Start

Create a new React project with the CLI:
Visit http://localhost:3000 to see your app!

Project Structure

How React Works with Mizu

When you build a React app with Mizu:
At runtime in production:
  1. User requests http://yourdomain.com
  2. Mizu serves index.html from embedded FS
  3. Browser loads React bundle
  4. React hydrates and takes over
  5. Client-side routing handles navigation
  6. API calls go to Mizu Go handlers

Backend Setup

app/server/app.go

app/server/routes.go

Frontend Setup

Entry Point

frontend/src/main.tsx

Why StrictMode?
  • Highlights potential problems in components
  • Warns about deprecated APIs
  • Detects unexpected side effects
  • Only in development, no production overhead

Root Component

frontend/src/App.tsx

Layout Component

frontend/src/components/Layout.tsx

React Hooks Deep Dive

React Hooks let you use state and other React features in function components.

useState

Manage component state:
Functional updates:

useEffect

Perform side effects:
Common patterns:

useContext

Access context values:

useReducer

Manage complex state:

useMemo

Memoize expensive calculations:

useCallback

Memoize callback functions:

useRef

Reference DOM elements or persist values:

Custom Hooks

Reuse stateful logic:
Usage:
More custom hooks:

React Router

Basic Routing

Nested Routes

Route Parameters

Programmatic Navigation

Protected Routes

State Management

Context API

Built-in state management:

Zustand

Lightweight state management:
Usage:

Data Fetching

React Query

Install:
Setup:
Usage:

SWR

Alternative to React Query:

Form Handling

React Hook Form

Install:
Basic usage:
With validation library:

Error Handling

Error Boundaries

Using react-error-boundary:

Styling

CSS Modules

Built into Vite:

Tailwind CSS

Install:
Configure tailwind.config.js:
Add directives to src/styles/index.css:
Usage:

Styled Components

Install:

Vite Configuration

frontend/vite.config.ts

Then use path aliases in components:
Update tsconfig.json:

Development Workflow

Start Development

Visit http://localhost:3000

Making Changes

Frontend changes:
  1. Edit any .tsx file in frontend/src/
  2. Save the file
  3. Browser updates instantly (HMR)
  4. State is preserved during updates
Backend changes:
  1. Edit .go files
  2. Stop server (Ctrl+C)
  3. Restart with go run cmd/server/main.go
Or use air for auto-reload:

React DevTools

Install browser extension: Features:
  • Inspect component tree
  • View props and state
  • Track component updates
  • Profiler for performance

Building for Production

Build the complete app:
This:
  1. Runs npm run build in frontend/
  2. Builds Go binary with go build
  3. Embeds frontend in binary
Output: ./bin/server (single executable) Run in production:

Build Optimizations

Code splitting:
Tree shaking:
Bundle analysis:

Performance Optimization

React.memo

Prevent unnecessary re-renders:

Virtual Lists

For large lists, use windowing:

Lazy Load Images

Real-World Example: Task Manager

Complete task management app:

Backend

Frontend Store

Components

Troubleshooting

HMR Not Working

Symptom: Changes don’t appear in browser Cause: HMR WebSocket not connecting through proxy Solution: Check vite.config.ts:

White Screen / Blank Page

Symptom: Production build shows blank page Cause: Base path mismatch or routing issues Solution 1: Check console for errors Solution 2: Ensure BrowserRouter (not HashRouter) Solution 3: Check Mizu’s SPA fallback is enabled

TypeScript Errors

Error: β€œCannot find module ’./App’” Solution: Check file extensions in imports:

404 on Page Refresh

Symptom: Direct URL works, but refresh gives 404 Cause: SPA fallback not configured Solution: Mizu handles this automatically, but ensure:

Large Bundle Size

Problem: Bundle is too large Solution:
  1. Analyze bundle:
  1. Enable code splitting:
  1. Check for duplicate dependencies:
  1. Use smaller alternatives:
  • Replace moment with date-fns
  • Replace lodash with individual functions
  • Consider Preact instead of React

When to Choose React

Choose React When:

βœ… You need the largest ecosystem and community βœ… Building a complex, interactive SPA βœ… TypeScript integration is important βœ… Team has React experience or you’re hiring React developers βœ… You need access to thousands of third-party libraries βœ… Job market considerations matter (most React jobs) βœ… You want industry-standard patterns and practices

Choose Something Else When:

  • Bundle size is critical β†’ Use Preact (3kB) or Svelte (2kB)
  • Learning curve matters β†’ Try Vue (easier to learn)
  • Need SSR/SSG β†’ Use Next.js (React framework)
  • Need full framework β†’ Use Next.js or Remix
  • Want simpler reactivity β†’ Try Vue or Svelte
  • Performance is paramount β†’ Try Svelte

Next Steps

Next.js Guide

Full React framework with SSR

API Integration

Best practices for API communication

Deployment

Build and deploy your app

Vue Guide

Try Vue instead of React

React Docs

Official React documentation

React Query

Powerful data fetching