Bird
Raised Fist0
Reactframework~5 mins

Managing large applications in React

Choose your learning style10 modes available
Introduction

Managing large applications helps keep your code organized and easy to understand. It makes adding new features and fixing bugs simpler and faster.

When your React app grows beyond a few components and files.
When multiple developers work on the same project.
When you want to reuse parts of your app in different places.
When you need to keep your app fast and easy to maintain.
When you want to separate concerns like UI, data, and logic.
Syntax
React
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Header from './components/Header';
import HomePage from './pages/HomePage';
import UserPage from './pages/UserPage';

function App() {
  return (
    <Router>
      <Header />
      <Routes>
        <Route path='/' element={<HomePage />} />
        <Route path='/user' element={<UserPage />} />
      </Routes>
    </Router>
  );
}

export default App;

Use folders to separate components, pages, and utilities.

Use routing to manage different screens or pages in your app.

Examples
This folder structure separates UI parts, pages, and helper code.
React
src/
  components/
    Button.jsx
    Header.jsx
  pages/
    HomePage.jsx
    UserPage.jsx
  utils/
    api.js
    helpers.js
  App.jsx
Keep data fetching inside page components or custom hooks to separate logic.
React
import React, { useState, useEffect } from 'react';

function UserPage() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    fetch('/api/user')
      .then(res => res.json())
      .then(data => setUser(data));
  }, []);

  if (!user) return <p>Loading...</p>;

  return <div>Welcome, {user.name}!</div>;
}
Create small reusable components for buttons, inputs, etc.
React
import React from 'react';

export function Button({ onClick, children }) {
  return <button onClick={onClick}>{children}</button>;
}
Sample Program

This example shows a simple React app with routing and separate components for header and pages. It simulates loading user data on the User page.

React
import React from 'react';
import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';

function Header() {
  return (
    <header>
      <nav>
        <ul>
          <li><Link to='/'>Home</Link></li>
          <li><Link to='/user'>User</Link></li>
        </ul>
      </nav>
    </header>
  );
}

function HomePage() {
  return <main><h1>Home Page</h1><p>Welcome to the home page.</p></main>;
}

function UserPage() {
  const [user, setUser] = React.useState(null);

  React.useEffect(() => {
    // Simulate fetching user data
    setTimeout(() => {
      setUser({ name: 'Alice' });
    }, 500);
  }, []);

  if (!user) return <main><p>Loading user info...</p></main>;

  return <main><h1>User Page</h1><p>Hello, {user.name}!</p></main>;
}

export default function App() {
  return (
    <Router>
      <Header />
      <Routes>
        <Route path='/' element={<HomePage />} />
        <Route path='/user' element={<UserPage />} />
      </Routes>
    </Router>
  );
}
OutputSuccess
Important Notes

Split your app into small components to keep code clean and reusable.

Use routing libraries like react-router to handle multiple pages or views.

Keep data fetching and side effects inside components or custom hooks.

Summary

Organize large React apps by splitting code into folders and components.

Use routing to manage different pages or views.

Keep logic and UI separated for easier maintenance and teamwork.

Practice

(1/5)
1. What is a common way to organize a large React application to keep it manageable?
easy
A. Use only class components to keep consistency
B. Write all code in a single file for easy access
C. Avoid using components and write plain JavaScript
D. Split the app into smaller components and folders

Solution

  1. Step 1: Understand component-based design

    React encourages breaking UI into small, reusable components.
  2. Step 2: Organize code in folders

    Grouping related components and files in folders helps maintain large apps.
  3. Final Answer:

    Split the app into smaller components and folders -> Option D
  4. Quick Check:

    Component + folder structure = A [OK]
Hint: Think modular: small parts in folders make apps manageable [OK]
Common Mistakes:
  • Putting all code in one file makes it hard to maintain
  • Ignoring components loses React's main advantage
  • Using only class components is outdated
2. Which of the following is the correct way to import a component named Header from a file in React?
easy
A. import Header from './Header';
B. require Header from './Header';
C. import { Header } from './Header.js';
D. include Header from './Header';

Solution

  1. Step 1: Recognize ES6 import syntax

    React uses ES6 modules, so import Header from './Header'; is correct for default exports.
  2. Step 2: Check other options

    require is CommonJS, not standard in React apps; include is invalid; named import { Header } requires named export.
  3. Final Answer:

    import Header from './Header'; -> Option A
  4. Quick Check:

    Default import uses import X from [OK]
Hint: Use ES6 import syntax: import Component from 'path' [OK]
Common Mistakes:
  • Using require instead of import in React
  • Confusing default and named imports
  • Using invalid keywords like include
3. Given this React component structure:
src/
  components/
    Navbar.js
  pages/
    Home.js
    About.js
  App.js

Which statement best describes how routing helps manage this app?
medium
A. Routing allows switching between Home and About pages without reloading
B. Routing bundles all components into one file for faster loading
C. Routing automatically converts components to class components
D. Routing disables navigation to keep app simple

Solution

  1. Step 1: Understand routing purpose

    Routing lets users move between different views (pages) in a React app without full page reloads.
  2. Step 2: Apply to given structure

    Home.js and About.js are pages; routing switches between them smoothly.
  3. Final Answer:

    Routing allows switching between Home and About pages without reloading -> Option A
  4. Quick Check:

    Routing = page switching without reload [OK]
Hint: Routing changes views without refreshing the whole page [OK]
Common Mistakes:
  • Thinking routing bundles files together
  • Believing routing changes component types
  • Assuming routing disables navigation
4. You have this React code snippet:
function App() {
  const [count, setCount] = useState(0)

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

What is the main error preventing the button from working?
medium
A. setCount should be called without parentheses
B. Missing import of React
C. Using double quotes instead of curly braces for onClick handler
D. State variable count is not initialized

Solution

  1. Step 1: Check onClick syntax

    In JSX, event handlers must be passed as functions inside curly braces, not strings in quotes.
  2. Step 2: Identify the error

    Here, onClick="setCount(count + 1)" uses quotes, so React treats it as a string, not a function.
  3. Final Answer:

    Using double quotes instead of curly braces for onClick handler -> Option C
  4. Quick Check:

    JSX event handlers need curly braces [OK]
Hint: Use curly braces for event handlers, not quotes [OK]
Common Mistakes:
  • Using quotes instead of curly braces for onClick
  • Forgetting to import React (less common in new versions)
  • Calling setCount incorrectly
5. In a large React app, you want to keep UI components separate from business logic for easier teamwork. Which approach best supports this?
hard
A. Avoid using hooks and manage state globally only
B. Use container components to handle logic and presentational components for UI
C. Write all logic inside UI components to keep code together
D. Put all components and logic in one file to reduce imports

Solution

  1. Step 1: Understand separation of concerns

    Separating UI and logic helps teams work independently and keeps code clean.
  2. Step 2: Apply container and presentational pattern

    Container components manage data and logic; presentational components focus on UI rendering.
  3. Final Answer:

    Use container components to handle logic and presentational components for UI -> Option B
  4. Quick Check:

    Separate logic and UI with containers and presentational components [OK]
Hint: Split logic (containers) and UI (presentational) components [OK]
Common Mistakes:
  • Mixing logic inside UI components makes maintenance hard
  • Avoiding hooks limits React's power
  • Putting everything in one file causes confusion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard