Lesson 2

Discovering the Cosmos of React Components: From Class to Functional

Topic Overview and Actualization

Welcome back, space traveler! As our journey surges forward, we are discovering more about the universe of React. Today, we will delve into React components, define Class and Functional components, learn why we use the latter, and create complex functional components with JSX. Let's go!

Introduction to Class Components

Class components, which were once vital in React, are a type of ES6 class. These components are robust, offering lifecycle methods, render methods, constructors, and the ability to handle state and props.

Below is an example of a simple Class Component:

1class Hello extends React.Component { 2 render() { // The render function determines what gets displayed 3 return <h1>Hello, world!</h1>; // Hello, world! will be shown on the screen 4 } 5}
Decline of Class Components

Class components, however, faced limitations. Having to handle the this keyword complicated matters and resulted in more bugs. As JavaScript evolved, developers started preferring functional components due to their simplified syntax.

Introduction to Functional Components

Functional components have been a part of React from the very beginning and are simply JavaScript functions. They accept props object as parameters and implement the render method to return React elements. These could be anything: a fragment of HTML, another functional component, or a class component.

But there's a crucial thing to remember. Every functional component must return only one parent element.

You might wonder, "What if I want to return multiple elements?" That's where wrapping elements come in handy. If your component needs to return multiple elements, they must all be wrapped in a parent element. This parent could be a div, a section, or any other HTML element.

Here's the example:

1function Welcome(props) { 2 return ( 3 <div> 4 <h1>Hello, {props.name}</h1>; 5 <p>Welcome to the React Universe!</p> 6 </div> 7 ); 8}

But what if you don't want an additional div in your HTML? This is where React Fragment comes into play! Fragments let you group a list of children without adding extra nodes to the DOM. Let's use React Fragment to refactor our Welcome component:

1function Welcome(props) { 2 return ( 3 <> 4 <h1>Hello, {props.name}</h1>; 5 <p>Welcome to the React Universe!</p> 6 </> 7 ); 8}

In this way, you can return multiple elements from a component. Functional components have shown their incredible power and versatility especially after the introduction of Hooks, as we will see going forward.

Using Variables in JSX

Now, let's dive deeper into how we can use variables inside our JSX in functional components. When JSX sees curly braces {}, it knows that it needs to interpret the contents in JavaScript mode. This means we can insert any valid JavaScript expression within those braces!

JavaScript expressions can be variables, operations, functions or any other valid piece of code that has a value. Here's a bit more about what we can put into these curly braces {}:

  • Variables - We can access JavaScript variables inside curly braces and their values will be interpolated into the resulting HTML.

  • Expressions - Any valid JavaScript expression that results in a value can be placed inside curly braces. This can include arithmetic operations, function calls, property access, etc.

  • Functions - We can even run JavaScript functions inside our JSX, which needs to return a valid JSX or a value that can be rendered, such as a string, number etc.

Let's see an example that demonstrates all these points:

1const someFunction = () => 5; 2 3function Welcome() { 4 // Define some variables 5 const salutation = "Hello,"; 6 const object = "Universe!"; 7 8 // Here we use a variable and a ternary operation inside the curly braces 9 return (<div> 10 <p>Our someFunction returns {someFunction();}</p> 11 <h1>{salutation} { object === "Universe!" ? "How vast you are!" : "Who are you?" }</h1> 12 </div>); 13} 14 15// Usage: <Welcome />

In this component, we have variables as well as a ternary operation inside the curly braces in JSX. Although it's a fairly simple example, it shows the power and flexibility that JSX affords you when building user interfaces in React.

Deep Dive into Functional Components

Functional components tend to be easier to read, test, and debug than their class-based counterparts. As they are purely JavaScript functions, they have the ability to return various types of data: components, arrays, strings, numbers, Booleans, null, and more.

Let's look at a slightly more complex Functional Component with button element:

1const Greeting = () => { 2 return ( 3 <div> 4 <button onClick={() => alert(greeting)}>Greet Me!</button> {/* On click, show an alert with the greeting */} 5 </div> 6 ); 7}
Understanding Component Lifecycle

Like humans, React components have a lifecycle: they are created (Mounting), updated (Updating), and finally removed (Unmounting).

  • Mounting: The component is created and inserted into the DOM.
  • Updating: The component updates due to changes in its state, which we will discuss later.
  • Unmounting: The component is removed from the DOM.

React Hooks allow Functional components to have similar lifecycle capabilities to those of Class components.

Lesson Summary

Well done! Today we grasped the concepts of Class and Functional components, understood why we prefer Functional components, and created a complex functional component. We have also been introduced to the Component Lifecycle.

This knowledge is crucial for front-end developers as it helps us to write efficient and clear code.

Upcoming Practice Exercises

Ready for more adventure? Your hands-on practice sessions are up next. Here, you'll get to test and perfect your new skills in React components. Remember, practice makes perfect in coding. Onwards, to the next set of mysteries in React!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.