React Blog

·

4 min read

Knowing the Fundamentals of React React is important beause it enables programmers to create UI components that are reusable across the application. In this phase Class and functional components were covered, with an emphasis on functional components since they are the norm in contemporary React development because of their power and simplicity in using hooks.

Here’s a simple functional component example:

import React from 'react';

function Greeting({ name }) {

return

Hello, {name}!

; }

export default Greeting;

This Greeting component takes a name prop and renders a message. Props are crucial in React, as they allow components to be dynamic and adaptable based on the data they receive.

State Management with React Hooks Managing state in React is key to building interactive applications. We learned about the useState and useEffect hooks, which are essential for managing state and side effects in functional components. The useState hook lets us add state to functional components, while useEffect allows us to handle side effects such as data fetching, subscriptions, and manually updating the DOM.

Here’s an example of a simple counter component using useState:

import React, { useState } from 'react';

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

return (

Current count: {count}

<button onClick={() => setCount(count + 1)}>Increment

); }

export default Counter;

This counter increments its value each time the button is clicked, demonstrating how state can be updated dynamically in response to user actions.

Building a Book Review Application .One of the labs we tackled was a Book Review Application, where users could browse through a list of books, view detailed information about each book, and add reviews. This project solidified our understanding of component structure, state management, and data fetching.

We started by designing the main components: BookList, BookDetails, and ReviewForm. The BookList component displayed a list of books fetched from an API, while BookDetails showed more information about a selected book. The ReviewForm allowed users to submit their reviews, which were then displayed below the book details.

Here’s a snippet from the BookList component:

javascript Copy code import React, { useEffect, useState } from 'react';

function BookList({ onSelectBook }) { const [books, setBooks] = useState([]);

useEffect(() => { fetch('https://example.com/api/books') .then(response => response.json()) .then(data => setBooks(data)) .catch(error => console.error('Error fetching books:', error)); }, []);

return (

{books.map(book => ( <div key={book.id} onClick={() => onSelectBook(book)}>

by {book.author}

))} ); }

export default BookList;

This component fetches book data from an API and passes the selected book to the onSelectBook function, allowing the parent component to manage which book is currently being viewed.

Advanced Concepts: Routing and Component Communication As we progressed, we dove into more advanced concepts like React Router for handling navigation within the app. React Router enables us to create single-page applications with multiple views by mapping URLs to components.

We set up routing in our book review application, allowing users to navigate between different pages such as the book list, book details, and review form. Here’s how we set up the routing:

Each route corresponds to a different view, and the BookDetails component is dynamically rendered based on the book ID in the URL.

Building an Art Gallery Project, which involved displaying a collection of artworks, allowing users to add pieces to their cart or wishlist, and view details about each artwork. This project emphasized the importance of, props, and conditional rendering.

In the Gallery component,i managed the selected artwork state and conditionally rendered the ArtworkDetails component when a piece was selected. Here’s a snippet showing how i handled this:

import React, { useState } from "react";

import ArtworkDetails from "./ArtworkDetails";

function Gallery({ arts, addToMyCart, addToMyWishList }) { const [selectedArt, setSelectedArt] = useState(null);

return (

{arts.map((art) => ( <div key={art.id} onClick={() => setSelectedArt(art)}>

{art.title}

{art.title}

))} {selectedArt && ( )} ); }

export default Gallery;

This project helped me understand how to structure applications with multiple interconnected components, ensuring data flows smoothly from parent to child components.

Conclusion Over the last two weeks, we’ve covered a lot of ground in React development. From mastering state management and data fetching to building complex applications with routing and component communication, i’ve gained a solid foundation in React. These projects have not only reinforced my understanding of React’s core concepts but have also provided me with practical experience that will be invaluable in future development endeavors.

Whether you’re building a simple app or a full-featured web application, React’s flexibility and powerful features make it an excellent choice for modern web development. As we continue to explore more advanced topics , my ability to create sophisticated, scalable applications will only grow.