Understanding React Components and Props — Part 1

When you start building applications with React, two of the most important concepts you’ll need to understand are components and props. These two ideas are at the core of how React works and will be used in nearly every part of your application.

In React, everything starts with components. A component is like a self-contained piece of your user interface. Think of it as a custom HTML element that you can reuse throughout your app. Just like how you might use “<button>” or “<div>” in HTML, React lets you create your own elements like “<Header />”, “<UserCard />”, or “<CommentBox />”. These components help keep your code organized, reusable, and easier to maintain.

There are two main types of components.

  1. functional components

  2. class components

Functional components are simple JavaScript functions that return JSX-the syntax that looks like HTML but is actually JavaScript. They are the most common type used today, especially when working with React Hooks. Class components, while less common now, are based on ES6 classes and allow for more advanced features like lifecycle methods. However, functional components paired with hooks have become the modern standard for most React development.

Now, once you understand what components are, the next step is learning how they communicate. That’s where props come in. Props (short for properties) are how you pass data from one component to another. They work similarly to function arguments in JavaScript. You send them into a component so it can use that data to render something or perform an action.

For example, imagine you have a component called “Greeting”. You might want it to display different names depending on who is logged in. Instead of hardcoding the name inside the component, you can pass it as a prop. This makes your component flexible and reusable across different parts of your app. When using props, you’re essentially giving your component instructions on what to display or how to behave.

Props can carry all kinds of data-strings, numbers, arrays, objects, even functions. This flexibility allows you to build powerful, dynamic interfaces. For instance, you could pass a list of items to a component and let it render them, or pass a function that runs when a button is clicked. The possibilities are endless.

It’s important to remember that props should never be changed inside a component. In React, data flows in one direction-from parent to child and modifying props directly can lead to unexpected behavior and bugs. If you need to change some data within a component, that’s where state comes in, which is another key concept in React.

To make your components even more reliable, you can define default props. These are fallback values that get used if no value is provided for a particular prop. Default props help ensure your component behaves correctly even when not all data is passed in, making it more robust and predictable.

Mastering components and props is essential for any React developer. These concepts form the foundation of how React apps are structured and how data flows between different parts of the UI.

1
Subscribe to my newsletter

Read articles from Chamali L Liyanage directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chamali L Liyanage
Chamali L Liyanage

I am currently a student at ESOFT Metro Campus, pursing a HND in Software Engineering. I eager to learn new technologies and improve my skills.