Topic Overview and Actualization

Hello there, adventure-loving coder!

Today is a big day, where we venture deeper into the fascinating cosmos of React, an immensely popular JavaScript library used for creating modern user interfaces. On today's journey, we'll focus on understanding and applying methods, nesting functional components, and passing primitive data and methods as props.

Here's a refresher on some basics. A functional component in React is a JavaScript function that accepts props (short for properties) as an argument and returns a React element.

Imagine building a starship, where each functional component represents a different part, such as the thrusters or the cockpit. In our cockpit (which we'll regard as a functional component), we will define methods that act as controls for the thrusters. With props, we can pass control from the cockpit to the thrusters. Fascinating, isn't it?

Let's break it down!

What Are Methods, and How Do We Define Them in React?

Methods in JavaScript are actions associated with an object. They are blocks of code that perform specific tasks. These methods are defined inside of a functional component before the return statement, as shown below:

Using Methods in Functional Components

An advantage of methods is they can handle virtually any type of event, ranging from a simple mouse click, to more elaborate ones such as form submissions, providing a React component with dynamic functionality.

Let's extend the functionality of our spaceship. How about we add a method that checks the spaceship's fuel level?

Nesting Functional Components

Sometimes, a component might be designed to exist within another component. This is called "nesting". In our spaceship example, the thruster could be seen as a component nested within our spaceship component:

Great! Now our spaceship has thrusters!

Passing Primitive Data Types as Props

Now that the thruster is in place, how can we control it from the spaceship? Here's where props come into play. Props allow us to pass values between components. Let's pass a message — a data prop — from our Spaceship to our Thruster:

Passing Methods as Props

Beyond data, we can pass methods as props in React. This is especially useful when coordinating between components. Let's illustrate this concept:

In this example, the Thruster can now use the startThrusters method that's actually defined in the Spaceship component. When the button in the Thruster component is clicked, it will call startThrusters, and print "Thrusters are ON" to the console.

Destructuring Props in Function Signature

You might have noticed that accessing props repeatedly can result in repetitive code. In JavaScript, we have a neat language feature to simplify this: destructuring assignment. This lets us unpack properties from objects or arrays right in our function signature.

What if we analysed the engine status in our Thruster? Say our parent Spaceship component passes an engineStatus prop, we can access it directly by destructuring it from props:

You can see how much cleaner the destructuring version is. Now, we can access engineStatus directly, without having to write props. each time!

React and JavaScript provide many such tools to write clean and efficient code. As you continue exploring, you'll discover and master these. Now, let's continue with our functional components!

Lesson Summary and Practice

Great job! Today, we've deepened our understanding of how to use methods in functional components, how to nest components, and how to pass both primitive data and methods as props — all of which are crucial skills in your React journey.

Exciting practice activities are coming next. Remember that the key to becoming a skilled React developer lies in practice and experimentation. Brace yourself, because next up are real-life activities based on the concepts you've learned today!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal