DEV Community

Nwafor Onyebuchi
Nwafor Onyebuchi

Posted on

React for Total Beginners

🌱 A Step-by-Step, Active Learning Guide

🎯 Goal of This Guide

By the end, you will be able to:

  • Create a React project with Vite
  • Understand components, JSX, props, and state
  • Build simple interactive UI
  • Understand folder structure & common patterns
  • Be comfortable experimenting and reading errors

πŸ“¦ Prerequisites

None. You only need:

  • A computer
  • Node.js installed (you can check with: node -v in your terminal)

If Node isn't installed, get it here: https://nodejs.org

πŸ’‘ New to using the terminal?
You’ll need the command line to create and run your React app.
If you’ve never used the terminal before (or feel uncomfortable with it), start here: Command Lines For Beginners

πŸ‘‰ Beginner-Friendly Terminal Guide (Uniform for Windows/macOS/Linux)
Use this especially if you're on Windowsβ€”you’ll be guided to install Git Bash so all commands match perfectly.


πŸš€ SECTION 1 β€” Creating Your First React App with Vite

1.1 Install Vite + React

Open your terminal and type:

npm create vite@latest my-react-app --template react
Enter fullscreen mode Exit fullscreen mode

Then:

cd my-react-app
npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

Visit the local URL printed (usually http://localhost:5173).

πŸŽ‰ Your first React app is running!


🧠 Mini-Quiz (no pressure!)

  1. What command starts your Vite server?
  2. What tool did we use to create the React project?
  3. Where do you view your running app in the browser?

(Answer after you try!)


πŸ“ SECTION 2 β€” Understanding the Project Structure

Inside your project:

my-react-app/
 β”œβ”€ node_modules/     ← dependencies
 β”œβ”€ public/           ← static files
 β”œβ”€ src/
 β”‚   β”œβ”€ App.jsx       ← main component
 β”‚   β”œβ”€ main.jsx      ← React/Vite entry file
 └─ index.html
Enter fullscreen mode Exit fullscreen mode

Key files:

  • main.jsx: connects React to the HTML page
  • App.jsx: your first component

πŸ§ͺ Try This Exercise

Open App.jsx and change the text inside the <h1> tag.
Save β†’ see the browser auto-update.

If that works, you’ve successfully edited your first component!


⭐ SECTION 3 β€” Components (The Heart of React)

What is a Component?

A component is a function that returns UI.

Example:

function Welcome() {
  return <h2>Welcome to my app!</h2>;
}

export default Welcome;
Enter fullscreen mode Exit fullscreen mode

You can use components like HTML tags:

<Welcome />
Enter fullscreen mode Exit fullscreen mode

🎯 Activity: Create Your First Component

  1. In src/, create a file called Greeting.jsx
  2. Add:
function Greeting() {
  return <p>Hello! Nice to meet you πŸ‘‹</p>;
}

export default Greeting;
Enter fullscreen mode Exit fullscreen mode
  1. Open App.jsx and add:
import Greeting from './Greeting.jsx';

function App() {
  return (
    <>
      <h1>My First React App</h1>
      <Greeting />
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ You just composed UI using components!


🧠 Mini-Quiz

  1. What does a React component return?
  2. What file do you import new components into?
  3. What must every component function start with (uppercase or lowercase)?

πŸ’¬ SECTION 4 β€” JSX Basics (How React Writes HTML)

JSX allows you to write HTML inside JavaScript.

Examples:

const element = <h1>Hello</h1>;
Enter fullscreen mode Exit fullscreen mode

You can also insert variables:

const name = "Alex";
return <p>Hello {name}</p>;
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Code Completion Exercise

Fill the missing parts:

function FavoriteFood() {
  const food = "______";
  return <p>My favorite food is {______}.</p>;
}

export default ______;
Enter fullscreen mode Exit fullscreen mode

Try to complete it before checking answers.


πŸ”„ SECTION 5 β€” Props (Passing Data to Components)

Props let you send data from parent β†’ child.

Example child component:

function Greeting(props) {
  return <p>Hello, {props.name}!</p>;
}
Enter fullscreen mode Exit fullscreen mode

Parent:

<Greeting name="Sarah" />
Enter fullscreen mode Exit fullscreen mode

🎯 Activity: Make a Reusable Component

  1. Create a file UserCard.jsx
  2. Add:
function UserCard(props) {
  return (
    <div>
      <h3>{props.username}</h3>
      <p>Age: {props.age}</p>
    </div>
  );
}

export default UserCard;
Enter fullscreen mode Exit fullscreen mode
  1. Use it in App.jsx:
<UserCard username="Bob" age={30} />
<UserCard username="Luna" age={22} />
<UserCard username="Kai" age={19} />
Enter fullscreen mode Exit fullscreen mode

🧠 Mini-Quiz

  1. What direction do props flow?
  2. Are props read-only or editable inside a component?
  3. What object name usually holds received props?

πŸ”₯ SECTION 6 β€” State (Making Your App Interactive)

State lets your component remember values that change over time.

Example:

import { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <button onClick={() => setCount(count + 1)}>
      You clicked {count} times
    </button>
  );
}

export default Counter;
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Activity: Add a Counter to Your App

  1. Create Counter.jsx
  2. Copy the counter code above
  3. Import into App:
<Counter />
Enter fullscreen mode Exit fullscreen mode

Click the button β†’ count increases πŸŽ‰


🧠 Mini-Quiz

  1. What hook do we use for state?
  2. What does setCount do?
  3. Does changing state re-render the component?

🌈 SECTION 7 β€” Handling User Input

Example with an input field:

import { useState } from "react";

function NameInput() {
  const [name, setName] = useState("");

  return (
    <>
      <input
        value={name}
        onChange={(e) => setName(e.target.value)}
        placeholder="Type your name"
      />
      <p>You typed: {name}</p>
    </>
  );
}

export default NameInput;
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Try This Activity

Add <NameInput /> to App.jsx.

Then modify it so:

  • It displays text only if name length > 0
  • Bonus: If name is β€œReact”, show a special message

πŸ— SECTION 8 β€” Build a Small Project (Guided)

We’ll build a Simple To-Do List.

Step 1 β€” Create TodoApp.jsx

import { useState } from "react";

function TodoApp() {
  const [items, setItems] = useState([]);
  const [text, setText] = useState("");

  const addItem = () => {
    if (text.trim() === "") return;
    setItems([...items, text]);
    setText("");
  };

  return (
    <>
      <h2>Todo List</h2>
      <input
        value={text}
        onChange={(e) => setText(e.target.value)}
        placeholder="Add item"
      />
      <button onClick={addItem}>Add</button>

      <ul>
        {items.map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>
    </>
  );
}

export default TodoApp;
Enter fullscreen mode Exit fullscreen mode

Step 2 β€” Add to App.jsx

<TodoApp />
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ You’ve built your first real interactive React app!


🧠 Final Quiz (to check understanding)

  1. What is the difference between props and state?
  2. What does useState return?
  3. What is JSX?
  4. How do you create a new component?
  5. How do you handle user input?
  6. What happens when state changes?
  7. Why must component names start with a capital letter?

πŸ“š What’s Next After This Guide

  • Learn about conditional rendering
  • Learn about lists & keys
  • Learn about useEffect
  • Explore React Router for pages
  • Learn fetching APIs

Top comments (0)