Lesson 50/100

Tutorials React.js Tutorial

package.json & Tooling — Complete Guide

package.json & Tooling — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.

On this page

React.js Tutorial · Lesson 50 of 100

Enterprise State Architecture

Beginner ✓Intermediate ✓AdvancedProfessional

Advanced · 3 — Production skills · ~18 min read · Module 5: API & State Management

Introduction

This is advanced material: Enterprise State Architecture. It is what teams use on live products. Read the example carefully and try changing one line at a time to see what happens. State architecture is deciding what lives where: local useState, Context, Zustand, Redux, or TanStack Query. Server data and UI state are usually separated. Mixing everything in one global store causes bugs. Clear rules help teams scale.

State mistakes cause the most React bugs for beginners. Keep server data and UI state separate — this lesson shows how teams do that.

When will you use this?

Use these patterns when data is shared across pages or comes from a server.

  • Shopping carts, user sessions, and API data need clear state patterns.
  • Teams pick Context, Zustand, or Redux based on app size — you will see all three in jobs.

Real-world: where state lives

Architecture doc for SaaS: server data in Query, UI filters in Zustand, form drafts in local useState, auth in Context.

Production-style code

/*
  Toolliyo state map:
  - TanStack Query → courses, enrollments, quiz results (server)
  - Zustand → sidebar open, active filter chips (client UI)
  - useState → single form field focus, modal open (local)
  - Context → user session, theme (app-wide, low churn)
  - NOT in Redux → avoid duplicating Query cache in Redux
*/
function CoursePage({ id }) {
  const { data: course } = useQuery({ queryKey: ['course', id], queryFn: () => fetchCourse(id) });
  const filter = useFilterStore(s => s.tag);
  const [tab, setTab] = useState('outline');
}

What happens in production: Teams avoid "everything in Redux" anti-pattern — clearer mental model and fewer bugs at scale.

Lesson example (start here)

Copy this smaller example first. Once it works, compare it with the real-world code above.

// Server data → TanStack Query
// Global UI (theme, sidebar) → Context or Zustand
// Form typing → local useState
// Cart (needs persist) → Zustand or Redux

Line-by-line walkthrough

CodeWhat it means
// Server data → TanStack QueryComment — notes for humans; the computer ignores it.
// Global UI (theme, sidebar) → Context or ZustandComment — notes for humans; the computer ignores it.
// Form typing → local useStateComment — notes for humans; the computer ignores it.
// Cart (needs persist) → Zustand or ReduxComment — notes for humans; the computer ignores it.

How it works (big picture)

  • Ask: does this come from the API?
  • Use Query.
  • Is it only this screen?
  • useState.
  • Shared across app?
  • Context/Zustand/Redux.

Do this on your computer

  1. List all data types in your app.
  2. Assign each to local, server, or global client state.
  3. Avoid duplicating server data in Redux.
  4. Read the real-world section and name which part of the app uses this topic.
  5. Run the example locally and confirm the same behavior in the browser.
  6. Change one value in the example (text, initial state, or URL) and predict what will happen before you save.

Experiments — try changing this

  • Change text or labels in the example and save — watch the browser update.
  • Break the code on purpose (remove a bracket), read the error message, then fix it.
  • Change the initial state value and see the starting UI change.
  • Open React DevTools (browser extension) while running Enterprise State Architecture and inspect component props/state.

Remember

Server state → TanStack Query. Local UI → useState. Shared client state → Context/Zustand/Redux by complexity.

Common questions

One library for everything?

No — use the right tool per data type.

How long should I spend on Enterprise State Architecture?

Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.

What if I get stuck on Enterprise State Architecture?

Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.

Where is Enterprise State Architecture used in real jobs?

See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Example of mocking fetch in a test: import { render, screen, waitFor } from '@testing-library/react'; import MyComponent from './MyComponent'; test('fetches and displays data', async () => { global.fetch = jest.fn(() => Promise.resolve({ json: () => Promise.resolve({ message: 'Data loaded' }), }) ); render(<MyComponent />);
Short answer: wait waitFor(() =&gt; screen.getByText('Data loaded')); expect(screen.getByText('Data loaded')).toBeInTheDocument(); }); Here, we mock the fetch function to return mock data, then test whether it appears in…
Mid PDF Detailed
React Developer Tools:?
Short answer: Use the Profiler tab to measure the render times and identify slow components. Check for unnecessary re-renders and optimize with React.memo or shouldComponentUpdate. Real-world example (ShopNest) ShopNest’…
Mid PDF Detailed
Mock axios:?
Short answer: import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data' }); Example code import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data'…
Mid PDF Detailed
Choose a Testing Library: Use libraries like Jest (test runner) and React Testing?
Short answer: Library (for testing React components). Real-world example (ShopNest) ShopNest’s storefront is React: components for ProductCard, CartDrawer, and CheckoutForm, with hooks for local UI state. Say this in the…
Mid PDF Detailed
Only call hooks at the top level?
Short answer: (Don’t call hooks inside loops, conditions, or nested functions.) Real-world example (ShopNest) ShopNest’s cart icon uses useState for count and useEffect to load the cart once on mount. Say this in the int…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details