Getting Started with React as a Python User


All of the interactive applets in the Interactive Simulations Gallery are powered client-side by React components embedded inside Astro (see About the Stack for how the rest of the site fits together). If your background is mostly in Python, React clicks quickly once you map its primitives to familiar Python concepts.

Python Mental Model → React Equivalents

Python ConceptReact EquivalentHow It Maps
Pure Functions (def)ComponentsIn Python, functions process data and return values. In React, components are functions that accept props (like kwargs) and return UI markup (JSX).
Mutable VariablesuseState()Updating a normal Python variable doesn’t automatically redraw a screen. useState() creates reactive variables that trigger a UI re-render whenever their setter is called.
f-strings / Jinja2JSXJSX mixes HTML tags with JavaScript expressions inside { curly braces }, feeling very similar to Python f"{expr}" or Jinja2 templates.
Event Loops / CallbacksuseEffect()Runs side effects—such as fetching live API data, setting timers, or driving animation frames—in response to component mount or state changes.

Side-by-Side Example: A Stateful Counter

In Python, you might represent state in a small class:

class Counter:
    def __init__(self, initial: int = 0):
        self.count = initial

    def increment(self) -> None:
        self.count += 1

In React, you express the state and its rendered output together as a function component:

import React, { useState } from "react";

export default function InteractiveCounter({ initial = 0 }) {
  const [count, setCount] = useState(initial);

  return (
    <button onClick={() => setCount(count + 1)}>
      Count is {count}
    </button>
  );
}

Every time the button calls setCount(count + 1), React re-runs InteractiveCounter with the updated count and updates only the changed DOM text.

Quickstart for Python Developers

  1. Install the JavaScript Runtime: Install Node.js (LTS), which gives you both the node runtime and npm / npx (JavaScript’s equivalent to pip and uvx).
  2. Scaffold a React App with Vite:
    npm create vite@latest my-app -- --template react-ts
    cd my-app
    npm install
    npm run dev
  3. Think in State, Not Manual DOM Edits: Define reactive values with useState and derive the UI from them instead of reaching in and mutating elements imperatively.
  4. Pair with Python Backends: Keep heavy data work in Python (FastAPI, Flask, or DuckDB/Polars endpoints) and let a lightweight React frontend handle live interactivity and visualizations.

Reference: Installing Node.js, npm & React

1. Installing JavaScript (Node.js & npm)

2. Official React Learning Resources

  • Official React Docs: react.dev — interactive tutorials and API reference.
  • Try the live components: Browse the Interactive Simulations Gallery to see these patterns driving real-time canvas simulations, transit dashboards, and macroeconomic models.