DEV Community

Cover image for React: Understanding Props and Components
Kevin
Kevin

Posted on • Updated on

React: Understanding Props and Components

Understanding props, components, and the transfer of props from parent to child. If you're new to React like I am, you might be thinking to yourself "What does this all mean"?

Oppenheimer Thinking

When you begin with React, think of it like working with a special kind of puzzle. In this puzzle, each piece can change and update itself. It's a way to build websites where parts of the page can change without needing to replace the whole puzzle. Then you might think to yourself, again. "What"?

Shocked fan

Well, if you're familiar with JavaScript, you know that an object is a kind of variable that holds multiple values as key-value pairs, known as properties, which we'll be looking at soon. Let's use a dating app profile as an example.

A profile object might have properties like name, age, and hobbies, each representing a piece of information about the user. These properties are accessed using dot notation. If you're not already familiar with JavaScript, don't worry—understanding this concept is helpful since React is a library built on JavaScript.

Dot Notation Example:

Dot Notation Example

In this example, the profile object represents a user on a dating app. We use the dot notation to grab the name, age, and hobbies properties of the profile. Simply by writing the name of the object, followed by the (.) and then the properties of said object.

In reality, this is similar to how you would view different details of someone's dating profile on an app like Tinder, Bumble, or Hinge. Understanding how to access properties in JavaScript objects is similar to how you'll use props to pass data in React components as you'll soon see.

In React, props work similar to objects in JavaScript. They are used to pass data from one component to another. It's useful to think of props as the arguments that a component accepts, provided using JSX syntax, much like how you'd set attributes in HTML. When writing a function for a component, you use the keyword 'props' to handle this passed data.

Props Example

Match Notification Component

  • MatchNotification is a functional component that takes props as its only argument.

  • It uses a conditional rendering based on props.hasNewMatch. If hasNewMatch is true, it displays a message about a new match; otherwise, it shows a message indicating there are no new matches.

  • props.matchName is used to display the name of the new match.

In our MatchNotification component, we use props to make decisions about what to display.

Understanding Destructuring in React Components

While the MatchNotification component uses the props object directly, another common pattern in React is called destructuring. This approach can make your code cleaner and more intuitive, especially when a component deals with multiple props.

In React, props work similarly to objects in JavaScript as you might have already started to notice (recall the dot-notation example). They are used to pass data from one component to another. This concept becomes especially clear when we look at how the Profile component is structured in our example.

Child Component - Profile

In this example, a Profile component represents a user's profile. It receives data such as the user's name, age, and hobbies through props, very similar to what we did with the JavaScript dot notation. These props are then used within the component to render the profile information dynamically.

Breaking Down the Profile Component:

  • In the Profile component, we use destructuring to directly extract properties from props: { name, age, occupation, location, interests }.

  • This allows us to access each piece of the user information directly, without repeatedly prefixing them with props.

Using Props Inside JSX:

  • Inside the JSX, we use curly brackets {} to insert these JavaScript expressions, such as {name}, {age}, and {occupation}.

  • For example, {name} in the JSX will dynamically display the user's name.

  • The interests prop, an array, is joined into a single string for display.

Dynamic Rendering with Props:

Just like in a dating app, where each profile has unique details about a person, the Profile component can display various information based on the props it receives. This dynamic rendering is achieved through the props passed down from a parent component.

Parent-Child Relationship

Let's consider the parent component in our app, which uses the Profile component to display a user's profile:

Parent Component

In this example, the parent component App renders the Profile component, passing data such as name, age, occupation, location, and interests. This demonstrates the one-way data flow from parent to child in React, where App is the parent and Profile is the child.

Now, let's incorporate MatchNotification into the App component along with the Profile component for our user Charlie Kelly

Final App Component

In the final App component, we have two child components: MatchNotification and Profile. Each child component plays a different role in the App, showing the versatility of React components.

Final App Component Breakdown: How the App Component Integrates Child Components

The App component passes down specific data to each child component through props. For example, it provides the MatchNotification component with hasNewMatch and matchName, and the Profile component with user details.

This demonstrates how React's component structure allows for reusability and flexibility. Each component can be thought of as a building block, which when put together, forms the complete application.

Rendering in App Component:

  • Within the App's JSX, we see the usage of , which passes the relevant props to the MatchNotification component.

  • Similarly, passes the required props to the Profile component.

React's design emphasizes a one-way data flow, which is evident in this structure. The App component (parent) passes data down to its child components, but these children do not pass data back up to the parent.

This design makes the data flow in the application predictable and easier to debug, as data always flows in one direction.

In conclusion, through these examples, you can see how React allows for the creation of interactive and dynamic user interfaces. Each component can have its own state and logic, which can be combined to build a dynamic web application. Hopefully, this helps a bit in your journey with React. Cheers

Cheers

Top comments (0)