đŸ“Ļ React Props: A Visual Guide

-react-props:-a-visual-guide

This JavaScript function is broken. So what will happen if you try to use it?

1

If you call this function, you’re going to get a reference error which says, “a is not defined”.

And this makes sense, the sum function is using two values, a and b, but has no idea what they are.

To fix it, we need to add a and b as parameters and pass two numbers as arguments.

2

That’s how you pass data to a JavaScript function, but what about a React component?

A React component looks a lot like a plain JavaScript function, but unlike one, it returns and renders React elements, such as a button.

3

To call a React component and have it display those elements, we use it as if it was a custom HTML element, but written in JavaScript.

4

But how do we pass data to functions when they are called like this?

Using this HTML-like syntax, we can pass it any data we like as if it was a custom HTML attribute.

For example, if we wanted to add our own custom text to our button, we could add a text attribute and set its value equal to some string.

5

In the world of React, this custom attribute is what is known as a “prop”. And we can add as many props to our components as we like. They can be any JavaScript data type.

6

If we want to use the props we’ve passed down to our component, you might think that each one is passed as a separate argument.

7

But that’s not the case. Unlike a normal JavaScript function, all of these props are collected into one value, which is itself an object.

This single parameter is referred to and named “props”.

8

It can be named anything, but the convention is to call this parameter “props” because that’s what it contains–all of the values that are passed down to this component.

Another reason it makes sense to call these values “props” is because what we’ve passed down are turned into properties on an object.

Once we’ve passed down the data that we like to our component, they can be used inside that component with curly braces.

9

And a neat pattern to use if your components are small, is to destructure the props object.

By adding a set of curly braces in your parameters, you can use destructure props into individual variables that you can use directly.

10

Become a Professional React Developer

React is hard. You shouldn’t have to figure it out yourself.

I’ve put everything I know about React into a single course, to help you reach your goals in record time:

Introducing: The React Bootcamp

It’s the one course I wish I had when I started learning React.

Click below to try the React Bootcamp for yourself:

Click to join the React Bootcamp

Click to get started

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
how-to-create-a-localization-strategy-for-your-business

How to create a localization strategy for your business

Next Post
managing-the-balancing-act-of-pricing-|-vaishnavi-ravi

Managing the balancing act of pricing | Vaishnavi Ravi

Related Posts
debugging-in-wordpress-|-wordpress-āĻ-āĻ†āĻŽāĻŋ-āĻ¯ā§‡āĻ­āĻžāĻŦā§‡-āĻĄāĻŋāĻŦāĻžāĻ—āĻŋāĻ‚-āĻ•āĻ°āĻŋ

Debugging in WordPress | WordPress āĻ āĻ†āĻŽāĻŋ āĻ¯ā§‡āĻ­āĻžāĻŦā§‡ āĻĄāĻŋāĻŦāĻžāĻ—āĻŋāĻ‚ āĻ•āĻ°āĻŋ

āĻ•ā§āĻ˛āĻžā§Ÿā§‡āĻ¨ā§āĻŸ āĻāĻ° āĻœāĻ¨ā§āĻ¯ āĻšā§‹āĻ• āĻŦāĻž āĻ¨āĻŋāĻœā§‡āĻ° āĻ•āĻžāĻœā§‡āĻ° āĻĒā§āĻ°ā§Ÿā§‹āĻœāĻ¨ā§‡ āĻšā§‹āĻ•, āĻĒā§āĻ°āĻžā§Ÿ āĻ¸āĻŽā§Ÿ āĻ†āĻŽāĻžāĻĻā§‡āĻ°āĻ•ā§‡ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āĻ¨ āĻ¸āĻŽāĻ¸ā§āĻ¯āĻž Devbug/Investigation āĻāĻ° āĻĒā§āĻ°ā§Ÿā§‹āĻœāĻ¨ āĻĒāĻ°ā§‡āĨ¤…
Read More