Overview
Been uses Jotai for state management, a primitive and flexible state management library for React. Jotai uses atomic state units that can be composed together, providing a simple yet powerful approach to managing application state.Jotai was chosen over other state management solutions like Redux or Zustand because of its minimal boilerplate, excellent TypeScript support, and atomic approach that scales well with React’s concurrent features.
State Architecture
All application state is defined insrc/state/atoms.ts. The state is organized into three categories:
- Base Atoms - Primitive state containers
- Derived Atoms - Computed state based on other atoms
- Write-Only Atoms - Action handlers for state mutations
Base Atoms
These atoms hold the core application state:Raw Countries Atom
Stores the complete country dataset as a dictionary for fast lookups:Why a dictionary? Using
Record<string, Country> with ISO 3166 codes as keys enables O(1) lookup performance when checking country data.Selected Countries Atom
Persists the user’s country selections to localStorage:- Uses
atomWithStoragefromjotai/utilsfor automatic persistence - Stores only ISO 3166 country codes (not full country objects) to minimize storage size
getOnInit: trueensures data is loaded synchronously on first render- Immutable array type (
readonly string[]) prevents accidental mutations
Focus Atom
Tracks the currently focused country for map navigation:Derived Atoms
Derived atoms compute values based on other atoms and automatically update when dependencies change.Countries Atom
Combines raw country data with selection state:- Reads both
rawCountriesAtomandselectedCountriesAtom - Transforms the country dictionary into an array
- Enriches each country with its
selectedstatus - Automatically recalculates when either dependency changes
Regions Atom
Groups countries by region and calculates completion percentages:countriesAtom, creating a dependency chain:
Write-Only Atoms (Actions)
Write-only atoms handle state mutations with complex logic.Add Country Atom
Adds a country to the selection and focuses the map on it:- First parameter
undefinedmeans this atom has no read value - Second parameter is the write function that receives
get,set, and parameters - Checks for duplicates before adding
- Sets focus atom to trigger map navigation
- Creates a new array (immutability) when updating selections
Remove Country Atom
Removes a country from the selection:Usage in Components
Components interact with atoms using Jotai hooks:Reading State
UseuseAtomValue to read atom values:
Writing State
UseuseSetAtom for write-only atoms (avoids unnecessary re-renders):
State Flow Example
Here’s what happens when a user selects a country:Derived atoms recalculate
countriesAtomrecalculates to include the new selection statusregionsAtomrecalculates to update completion percentages
Benefits of This Approach
Minimal Boilerplate
Minimal Boilerplate
No reducers, action types, or dispatch functions needed. Define atoms and use them directly.
Automatic Dependency Tracking
Automatic Dependency Tracking
Jotai automatically tracks which atoms depend on others. When a base atom changes, all dependent atoms recalculate.
Built-in Performance
Built-in Performance
Only components that read a specific atom re-render when that atom changes. Fine-grained reactivity without manual optimization.
Type Safety
Type Safety
Full TypeScript support with proper type inference. The compiler catches type errors at build time.
Easy Testing
Easy Testing
Atoms are just functions that can be tested in isolation without complex setup.
Persistence Strategy
TheselectedCountriesAtom uses atomWithStorage to persist user selections:
Related Resources
Jotai Documentation
Official Jotai documentation and API reference
Map Integration
Learn how state drives the map visualization