React Quickstart
This quickstart guide will help you build your first React application in just 5 minutes. You’ll learn the fundamentals of React by creating an interactive counter component with state management.What You’ll Build
You’ll create an interactive counter application that demonstrates:- Component creation
- State management with hooks
- Event handling
- JSX syntax
- Component composition
Quick Setup
1
Create a new React project
Use Vite to quickly scaffold a new React project:This creates a new React project with all the necessary configuration.
2
Start the development server
Launch the development server to see your app:Open your browser to
http://localhost:5173. You should see the default Vite + React welcome page.3
Open your code editor
Open the project in your favorite code editor:You’ll be working primarily in the
src directory.Building Your First Component
Let’s build a counter component that increments when you click a button. This example uses React’suseState hook, which is the fundamental way to manage state in function components.
1
Import React hooks
Open The
src/App.jsx and start by importing the useState hook from React:useState hook is one of React’s most important features. It lets your components “remember” information between renders.2
Create your counter component
Replace the contents of Save the file and watch your browser automatically update with the new component!
src/App.jsx with this code:3
Test your counter
Click the button in your browser. The count should increment with each click!
Notice how the component re-renders automatically when state changes. This is React’s declarative magic - you just update the state, and React handles updating the UI.
Understanding the Code
Let’s break down what’s happening in your counter component:State Declaration
useState(0)creates a state variable initialized to0countis the current state valuesetCountis the function you call to update the state- When you call
setCount, React re-renders your component with the new value
JSX Syntax
- Use curly braces
{}to embed JavaScript expressions - Use
onClick(camelCase) instead ofonclick - You can use JavaScript expressions inside JSX like
{count}
Event Handling
- Listens for click events on the button
- Calls
setCountwith the new count value - Triggers a re-render with the updated state
Adding More Functionality
Let’s enhance your counter with additional features:1
Add increment and decrement buttons
Update your component to include more controls:Now you have three buttons: decrement, reset, and increment!
2
Add step size control
Let’s add another state variable to control the increment/decrement step:Now you can control how much the counter increments or decrements!
3
Create reusable components
Split your app into smaller, reusable components. Create a new file Now update You now have three independent counters, each maintaining their own state!
src/Counter.jsx:src/App.jsx to use multiple counters:Key React Concepts You’ve Learned
Components
Components are the building blocks of React applications. They’re JavaScript functions that return JSX:Props
Props are how you pass data to components:State with useState
TheuseState hook lets components remember values:
Event Handling
React uses camelCase event names and passes functions as handlers:JSX Expressions
Embed JavaScript in JSX using curly braces:Common Patterns
Conditional Rendering
Show different UI based on state:Lists and Keys
Render lists of items:Form Inputs
Handle form input with controlled components:Using React Hooks from the Source
React provides many built-in hooks. Here are some of the most commonly used ones from the actual React source code:Using useEffect for Side Effects
Using useRef to Access DOM Elements
Next Steps
Congratulations! You’ve built your first React application and learned the fundamentals. Here’s what to explore next:Learn Key Concepts
Dive deeper into components, props, state, and lifecycle
Explore Hooks
Master useState, useEffect, useContext, and more
API Reference
Explore the complete React API documentation
Tips for Success
Troubleshooting
Component not updating? - Make sure you’re usingsetState to update state, not modifying it directly.
Infinite re-renders? - Check that you’re not calling state setters directly in the render body. Use event handlers or useEffect instead.
Props not working? - Verify you’re passing and receiving props with the correct names. Remember that props are immutable.
Hooks error? - Hooks must be called at the top level of your component, not inside loops, conditions, or nested functions.
Additional Resources
- React Documentation - Official React documentation
- React DevTools - Browser extension for debugging
- Create React App - Official React starter toolkit
- Vite - Fast build tool with excellent React support