The Journey of #100DaysOfCode (@Amir_BouGhanem)

:closed_book: ๐——๐—ฎ๐˜† ๐Ÿด๐Ÿฌ - ๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ถ๐—ป๐—ด & ๐—ฅ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด ๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜ ๐—–๐—ผ๐—บ๐—ฝ๐—ผ๐—ป๐—ฒ๐—ป๐˜๐˜€ :closed_book:

React components are the building blocks of any React application. Components allow you to split the UI into independent, reusable pieces, and think about each piece in isolation. There are two main types of components in React: functional and class-based components.

Component Types

  • Functional Component: Functional components are simple JavaScript functions that return JSX (the UI structure). Since the release of React hooks, theyโ€™ve become the go-to method for creating components.
  • Class Component: Class components are older and more verbose than functional components. They were widely used before hooks were introduced. A class component extends React.Component and requires a render() method to return JSX.

To render a React component, use the ReactDOM.render() method to render components to the DOM. In index.js

100daysofcode lebanon-mug

2 Likes

:closed_book: ๐——๐—ฎ๐˜† ๐Ÿด๐Ÿญ - ๐—ฃ๐—ฟ๐—ผ๐—ฝ๐˜€ :closed_book:

Properties (known as Props in React) are arguments passed into React components. They are used to pass data from one component to another. In both functional and class components, props can be accessed as props.propName.
Please refer to the code to learn more about props!

100daysofcode lebanon-mug

3 Likes

:closed_book: ๐——๐—ฎ๐˜† ๐Ÿด๐Ÿฎ - ๐—ฆ๐˜๐—ฎ๐˜๐—ฒ ๐— ๐—ฎ๐—ป๐—ฎ๐—ด๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐˜„๐—ถ๐˜๐—ต ๐˜‚๐˜€๐—ฒ๐—ฆ๐˜๐—ฎ๐˜๐—ฒ ๐—›๐—ผ๐—ผ๐—ธ :closed_book:

State management is one of the most important concepts in React. The state allows components to keep track of dynamic data and render changes automatically when the state is updated. React provides two ways to manage state: using hooks (in functional components).

Definition of React Hooks

React Hooks are special functions introduced in React 16.8 that allow you to use state and other React features (like lifecycle methods) in functional components, which were previously only available in class components. Hooks simplify the way you manage state, handle side effects, and reuse logic across components. Built-in hooks include: useState, useEffect, and useContext.

Hooks follow a few key rules:

  • They must be called at the top level of a functional component.
  • They cannot be called inside loops, conditions, or nested functions.
  • They must be called inside React function components or custom hooks.

With the introduction of React hooks, functional components can now manage state using the useState hook. It allows you to add state to functional components without converting them to class components. Refer to the example below to learn more about useState.

100daysofcode lebanon-mug

2 Likes

:closed_book: ๐——๐—ฎ๐˜† ๐Ÿด๐Ÿฏ - ๐˜‚๐˜€๐—ฒ๐—˜๐—ณ๐—ณ๐—ฒ๐—ฐ๐˜ ๐—ถ๐—ป ๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜ :closed_book:

The useEffect hook is used to handle side effects in React functional components. Side effects can include things like:

  • Fetching data from an API
  • Manually updating the DOM
  • Setting up subscriptions or timers
  • Cleaning up resources

useEffect Simplified:

  • Effect Logic : This is where the side effect is placed. This can be an API call, a subscription setup, or any other logic you want to run after rendering.
  • Cleanup : This optional function is used to clean up the effect (such as canceling a subscription or clearing a timer) when the component unmounts or before the effect runs again.
  • Dependencies Array : This array specifies which values useEffect depends on. When these values change, the effect re-runs. If the array is empty (), the effect runs only once, after the initial render.

Key Concepts of useEffect

  1. Runs after every render by default: By default, useEffect will run after every render (initial render + re-renders).
  2. Control when it runs: By adding a dependencies array, you can control when the effect runs. For example:
  • An empty array [] runs the effect only after the first render (like componentDidMount).
  • Specific dependencies like [count] ensure the effect runs only when count changes.
  1. Cleanup logic: You can return a function from useEffect that will run when the component unmounts or before re-running the effect. This is useful for cleaning up side effects like subscriptions or timers.

useEffect is essential for managing side effects in functional components, making it a powerful tool in any React project!

100daysofcode lebanon-mug

2 Likes

:closed_book: ๐——๐—ฎ๐˜† ๐Ÿด๐Ÿฐ - ๐—›๐—ฎ๐—ป๐—ฑ๐—น๐—ถ๐—ป๐—ด ๐—˜๐˜ƒ๐—ฒ๐—ป๐˜๐˜€ ๐—ถ๐—ป ๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜ :closed_book:

In React, handling events is similar to handling events in regular HTML, but there are a few key differences:

  • React events are named in camelCase (onClick instead of onclick).
  • You pass a function as the event handler, not a string.

Explanation :

  • The onClick event handler listens for clicks on the button, and the handleClick function is triggered when the button is clicked.

Please refer to the code for deeper context!

100daysofcode lebanon-mug

code.pdf (126.3 KB)

2 Likes

:closed_book: ๐——๐—ฎ๐˜† ๐Ÿด๐Ÿฑ - ๐—›๐—ฎ๐—ป๐—ฑ๐—น๐—ถ๐—ป๐—ด ๐—™๐—ผ๐—ฟ๐—บ๐˜€ ๐—ถ๐—ป ๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜ :closed_book:

Forms in React work similarly to regular HTML forms but require more management, particularly when handling form state and submissions. React provides a way to manage controlled components where the formโ€™s input value is handled by React state.

Explanation :

  • State Management : The useState hook manages the form state. Here, name is the current state value and setName updates it.
  • Handling Input Changes : The onChange event handler listens for user input and updates the state accordingly.
  • Submitting the Form : The handleSubmit function is triggered when the form is submitted, and event.preventDefault() prevents the default form submission (i.e., prevents the page from refreshing).

100daysofcode lebanon-mug

1 Like

:closed_book: ๐——๐—ฎ๐˜† ๐Ÿด๐Ÿฒ - ๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜ ๐—ฅ๐—ผ๐˜‚๐˜๐—ฒ๐—ฟ :closed_book:

React Router is a powerful library used for handling navigation and routing in React applications. It enables your app to have multiple views or pages and lets you navigate between them without reloading the page (client-side routing). This makes the app more dynamic and responsive, improving the user experience.

100daysofcode lebanon-mug

1. Setting Up React Router

To start using React Router, you need to install it via npm or yarn:

๐—‡๐—‰๐—† ๐—‚๐—‡๐—Œ๐—๐–บ๐—…๐—… ๐—‹๐–พ๐–บ๐–ผ๐—-๐—‹๐—ˆ๐—Ž๐—๐–พ๐—‹-๐–ฝ๐—ˆ๐—†

2. Basic Components of React Router

React Router has several key components that are essential for navigation:

  • BrowserRouter : The top-level component that wraps the app and enables routing functionality.
  • Routes : A container component that holds all the route definitions.
  • Route : Defines a path and the component that should be rendered for that path.
  • Link : Used to create navigation links between routes without causing a page reload (unlike the traditional tag).

3. Route Parameters

React Router allows you to define dynamic routes with parameters, which can be accessed in the component.

code.pdf (311.4 KB)