Mada za sehemu hiiBuilding the front-end using a frameworkMada 3
- Implementing components and layouts
- Performing customisation of styles
- Implementing user interactions
Implementing Components and Layouts
In React, a component is a reusable block of code that controls part of a webpage, such as a header, footer, navigation bar, or message box. A layout is the complete page structure formed by combining multiple components together. Components make development faster because you write a piece of code once and use it many times throughout your application.
A functional component is a JavaScript function that returns JSX (JavaScript XML). JSX looks like HTML but is actually JavaScript that describes what the user interface should appear like. Functional components are the most commonly used type in modern React development because they are simple, easy to write, and support React Hooks for managing data and interactivity.
Props in Functional Components
Props (short for properties) are pieces of information passed from a parent component to a child component. They allow components to display different content without rewriting the component code.
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
The component can be used as: <Greeting name="Amina" /> to display "Hello, Amina!"
Hooks in Functional Components
Hooks are special functions that give functional components extra capabilities. The most common hook is useState, which lets components remember information that changes over time, such as user input or click counts.
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times.</p>
<button onClick={() => setCount(count + 1)}>Click Me!</button>
</div>
);
}
A class component is another way to define a React component using a JavaScript class. Class components were the standard approach before hooks were introduced. They use this.props to receive data and this.state to manage changing data.
import React, { Component } from 'react';
class Greeting extends Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
Modern React applications prefer functional components because they are simpler and more concise.
Let us create a simple page layout with three components: Header, MainContent, and Footer.
Step 1: Create the Header Component
Create a file named Header.js inside the src/ folder:
function Header() {
return (
<header style={{ backgroundColor: 'navy', color: 'white', padding: '10px', textAlign: 'center' }}>
<h1>Welcome to My React Page</h1>
</header>
);
}
export default Header;
Step 2: Create the MainContent Component
Create MainContent.js in the src/ folder:
function MainContent() {
return (
<main style={{ padding: '20px' }}>
<p>This is the main content of the page. You can write anything here.</p>
</main>
);
}
export default MainContent;
Step 3: Create the Footer Component
Create Footer.js in the src/ folder:
function Footer() {
return (
<footer style={{ backgroundColor: 'lightgray', padding: '10px', textAlign: 'center' }}>
<p>© 2025 My React App</p>
</footer>
);
}
export default Footer;
Step 4: Combine Components in App.js
Open src/App.js and import all three components to form the complete layout:
import React from 'react';
import Header from './Header';
import MainContent from './MainContent';
import Footer from './Footer';
function App() {
return (
<div>
<Header />
<MainContent />
<Footer />
</div>
);
}
export default App;
When you run the app with npm start, the browser displays a complete webpage with the header at the top, content in the middle, and the footer at the bottom.
React components can be styled using external CSS files, inline styles, or CSS modules. For beginners, external CSS files are the easiest option.
Using External CSS
Write styles in a separate .css file and import it into your component. Create Welcome.css:
h1 {
color: blue;
text-align: center;
}
Then import it in Welcome.js:
import React from 'react';
import './Welcome.css';
function Welcome() {
return <h1>Welcome to React</h1>;
}
export default Welcome;
Using Inline Styles
Write styles directly inside the component using the style attribute. Values must be written as JavaScript objects:
function Welcome() {
return (
<h1 style={{ color: 'green', textAlign: 'center' }}>
Welcome to React
</h1>
);
}
Follow these steps to practice creating components and layouts:
- Open your React project in Visual Studio Code.
- Inside the
src/folder, create three files:Header.js,MainContent.js, andFooter.js. - In each file, write a simple function that returns a message. For example, the Header can display "Welcome to My Site," the MainContent can describe your favourite subject, and the Footer can show "Created by [Your Name], 2025."
- Open
App.jsand import all three components. - Use the components inside the App function to form your full layout.
- Select real-world websites and discuss their component structures and layout strategies.
In Tanzania, students can use React components to build a school notice board website where the navigation bar, notice cards, and footer are created once as reusable components and displayed on every page. For example, a Form 3 student creating a project for their school's science club could use a component-based layout to display upcoming events and exam timetables consistently across multiple pages, making the site easy to maintain and update.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Create reusable components within the framework.
- Design and implement responsive layouts using framework-specific features.
Tathmini ya matokeo
Functional webpage with reusable components (e.g., headers, footers, cards) and responsive layouts.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how
- front-end frameworks (e.g., React, Angular) use component-based architecture.
Principles: The student should explain the principles of:
- modularity, reusability, and responsiveness.
Theories: The student should explain:
- concept of component hierarchies
- the role of state management in layouts.
Circumstantial knowledge:
Detailed knowledge about:
- challenges such as maintaining consistency across components and optimizing layout performance.
Swali
In React, what is a component?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado