Managing large applications helps keep your code organized and easy to understand. It makes adding new features and fixing bugs simpler and faster.
Managing large applications in 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.
src/
components/
Button.jsx
Header.jsx
pages/
HomePage.jsx
UserPage.jsx
utils/
api.js
helpers.js
App.jsximport 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>; }
import React from 'react'; export function Button({ onClick, children }) { return <button onClick={onClick}>{children}</button>; }
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.
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> ); }
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.
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
Solution
Step 1: Understand component-based design
React encourages breaking UI into small, reusable components.Step 2: Organize code in folders
Grouping related components and files in folders helps maintain large apps.Final Answer:
Split the app into smaller components and folders -> Option DQuick Check:
Component + folder structure = A [OK]
- Putting all code in one file makes it hard to maintain
- Ignoring components loses React's main advantage
- Using only class components is outdated
Header from a file in React?Solution
Step 1: Recognize ES6 import syntax
React uses ES6 modules, soimport Header from './Header';is correct for default exports.Step 2: Check other options
requireis CommonJS, not standard in React apps;includeis invalid; named import{ Header }requires named export.Final Answer:
import Header from './Header'; -> Option AQuick Check:
Default import usesimport X from[OK]
- Using require instead of import in React
- Confusing default and named imports
- Using invalid keywords like include
src/
components/
Navbar.js
pages/
Home.js
About.js
App.jsWhich statement best describes how routing helps manage this app?
Solution
Step 1: Understand routing purpose
Routing lets users move between different views (pages) in a React app without full page reloads.Step 2: Apply to given structure
Home.js and About.js are pages; routing switches between them smoothly.Final Answer:
Routing allows switching between Home and About pages without reloading -> Option AQuick Check:
Routing = page switching without reload [OK]
- Thinking routing bundles files together
- Believing routing changes component types
- Assuming routing disables navigation
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?
Solution
Step 1: Check onClick syntax
In JSX, event handlers must be passed as functions inside curly braces, not strings in quotes.Step 2: Identify the error
Here, onClick="setCount(count + 1)" uses quotes, so React treats it as a string, not a function.Final Answer:
Using double quotes instead of curly braces for onClick handler -> Option CQuick Check:
JSX event handlers need curly braces [OK]
- Using quotes instead of curly braces for onClick
- Forgetting to import React (less common in new versions)
- Calling setCount incorrectly
Solution
Step 1: Understand separation of concerns
Separating UI and logic helps teams work independently and keeps code clean.Step 2: Apply container and presentational pattern
Container components manage data and logic; presentational components focus on UI rendering.Final Answer:
Use container components to handle logic and presentational components for UI -> Option BQuick Check:
Separate logic and UI with containers and presentational components [OK]
- Mixing logic inside UI components makes maintenance hard
- Avoiding hooks limits React's power
- Putting everything in one file causes confusion
Ready to prove it?
Pass the test and this pattern moves unknown
