JavaScriptBeginner

React.js for Beginners

Build interactive UIs with React - components, JSX, hooks, and state management fundamentals.

8 hours
5 key concepts
3 in-depth sections
Official Docs

0 of 5 concepts completed (0%)

What You'll Learn

1.Components & JSX

jsx
function UserCard({ name, role, avatar, onSelect }) {
  return (
    <div
      className="card"
      onClick={() => onSelect(name)}
      role="button"
      tabIndex={0}
    >
      <img src={avatar || "/default.png"} alt={`${name}'s avatar`} />
      <h2>{name}</h2>
      <span className={`badge badge-${role}`}>{role}</span>
    </div>
  );

// JSX rules: // 1. className instead of class // 2. htmlFor instead of for // 3. CamelCase events: onClick, onChange, onSubmit // 4. All elements must close: ,
// 5. Single root (or <>... Fragment)

// Conditional rendering {isLoading ? : } {user && } {items.length > 0 ? : }

// Lists always provide stable key {items.map(item => )} ```

2.Essential Hooks

jsx

// useState const [user, setUser] = useState(null); setUser(prev => ({ ...prev, name: "Updated" })); // functional update

// useEffect with cleanup useEffect(() => { let mounted = true; async function load() { const data = await fetchUser(userId); if (mounted) setUser(data); } load(); return () => { mounted = false; }; }, [userId]);

// useRef mutable, no re-render const inputRef = useRef(null); useEffect(() => { inputRef.current?.focus(); }, []);

// useMemo expensive computation const sorted = useMemo( () => [...items].sort((a, b) => a.name.localeCompare(b.name)), [items] );

// useCallback stable function reference const handleClick = useCallback((id) => { setSelected(id); }, []);

// Custom hook function useLocalStorage(key, initial) { const [value, setValue] = useState( () => JSON.parse(localStorage.getItem(key) ?? "null") ?? initial ); useEffect(() => { localStorage.setItem(key, JSON.stringify(value)); }, [key, value]); return [value, setValue]; } ```

3.Context & State Management

jsx

// Context API const AuthContext = createContext(null);

function AuthProvider({ children }) { const [user, setUser] = useState(null); const login = async (credentials) => { const u = await authService.login(credentials); setUser(u); }; return ( setUser(null) }}> {children} ); }

function useAuth() { const ctx = useContext(AuthContext); if (!ctx) throw new Error("useAuth must be inside AuthProvider"); return ctx; }

// useReducer complex state function reducer(state, action) { switch (action.type) { case "fetch_start": return { ...state, loading: true, error: null }; case "fetch_success": return { loading: false, data: action.payload, error: null }; case "fetch_error": return { ...state, loading: false, error: action.payload }; default: return state; } }

const [state, dispatch] = useReducer(reducer, { loading: false, data: null, error: null }); ```

Code Example

import { useState, useEffect } from "react";

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

  useEffect(() => {
    document.title = `Count: ${count}`;
  }, [count]);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(c => c + 1)}>
        Increment
      </button>
    </div>
  );
}

export default Counter;

Pro Tips

  • 1Never mutate state directly. Always create new objects/arrays: setState(prev => ({...prev, field: value})).
  • 2useEffect dependency arrays are critical. An empty array [] means 'run once on mount'. Missing a dependency causes stale closure bugs.
  • 3Extract logic into custom hooks (useWindowSize, useLocalStorage) this makes components simpler and logic reusable.
  • 4Use React DevTools Profiler to find slow components. The most common fix is React.memo on expensive child components.

Topics Covered

ReactHooksComponents

Continue Learning

Explore the official documentation and deepen your knowledge with real-world practice.

Explore the Full Portfolio

This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.

The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.

For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.