Skip to content

Commit 3acfb9b

Browse files
React: Props - Update from class to function based components #4788 (#5911)
* React: Props - Update from class to function based components #4788 * React: Props - Update from class to function based components #4788 * Update props.md minor fixes * prettier formating and fixing the indenation ---------
1 parent b27eaff commit 3acfb9b

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

content/react/concepts/props/props.md

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: 'Props'
3-
Description: 'In React, components are able to use props, or "properties", to display and share data throughout the application. In other words, props is the information that gets passed from one component to another. Parent components can pass props to their child components, but not the other way around. Props can be many data types, including: - Numbers - Strings - Functions - Objects jsx'
3+
Description: 'In React, props are read-only data passed from parent to child components, enabling communication and behavior customization through values and functions.'
44
Subjects:
55
- 'Web Development'
66
Tags:
@@ -11,95 +11,125 @@ CatalogContent:
1111
- 'paths/front-end-engineer-career-path'
1212
---
1313

14-
In React, components are able to use props, or "properties", to display and share data throughout the application. In other words, props is the information that gets passed from one component to another.
14+
In React, components use **props** (short for "properties") to share and display data throughout the application. Props are passed from parent to child components only and cannot flow in the reverse direction.
1515

16-
Parent components can pass props to their child components, but not the other way around. Props can be many data types, including:
16+
With functional components, props are handled as parameters in the function definition, making the code simpler and more intuitive.
17+
18+
Props can be various data types, including:
1719

1820
- Numbers
1921
- Strings
2022
- Functions
2123
- Objects
24+
- Booleans
25+
- Arrays
2226

2327
## Syntax
2428

25-
```jsx
29+
```pseudo
2630
import React from 'react';
2731
28-
class ParentComponent extends React.Component {
29-
render() {
30-
return <ChildComponent prop1="Mike" prop2="piza">
31-
}
32+
function ParentComponent() {
33+
return <ChildComponent prop1="Mike" prop2="pizza" />;
3234
}
3335
3436
function ChildComponent(props) {
35-
return <h2>This is prop1: {props.prop1}. This is prop2: {props.prop2}.</h2>
37+
return <h2>This is prop1: {props.prop1}. This is prop2: {props.prop2}.</h2>;
3638
}
39+
40+
export default ParentComponent;
3741
```
3842

39-
## `this.props`
43+
## `Props` in Functional Components
4044

41-
Every component has something called `props`.
45+
In functional components, props are received directly as an argument, making them simpler and more intuitive compared to class-based components, which use `this.props`.
4246

43-
A component’s `props` is an object. It holds information about that component.
47+
### Class-Based Components
4448

45-
To see a component’s `props` object, you use the expression `this.props`. Here’s an example of `this.props` being used inside of a render method:
49+
```js
50+
class Greeting extends React.Component {
51+
render() {
52+
// Printing the props object
53+
console.log(this.props);
54+
55+
return <h1>Hello world</h1>;
56+
}
57+
}
58+
```
4659

47-
```jsx
48-
render() {
60+
### Function-Based Components
61+
62+
```js
63+
function Greeting(props) {
64+
function Greeting(props) {
4965
// Printing the props object
50-
console.log(this.props);
66+
console.log(props);
5167

5268
return <h1>Hello world</h1>;
5369
}
5470
```
5571
5672
## Pass `props` to a Component
5773
58-
You can pass information to a React component. How? By giving that component an attribute:
74+
Information can be passed to a React component by giving it an attribute:
5975
6076
```js
6177
<MyComponent foo="bar" />
6278
```
6379
64-
Let’s say that you want to pass a component the message, `"This is some top secret info."`. Here’s how you could do it:
80+
For example, to pass a message, `"This is some top secret info."`, it can be done like this:
6581
6682
```js
6783
<Example message="This is some top secret info." />
6884
```
6985
70-
As you can see, to pass information to a component, you need a name for the information that you want to pass.
86+
To pass information to a component, a name is assigned to the information that is being passed.
7187
72-
In the above example, we used the name `message`. You can use any name you want.
88+
In the above example, the name `message` is used, but any name can be chosen.
7389
74-
If you want to pass information that isn’t a string, then wrap that information in curly braces. Here’s how you would pass an array:
90+
For non-string information, wrap the data in curly braces. Here’s how an array can be passed:
7591
7692
```js
7793
<Greeting myInfo={['top', 'secret', 'lol']} />
7894
```
7995
80-
In this next example, we pass several pieces of information to `<Greeting />`. The values that arent strings are wrapped in curly braces:
96+
In this next example, several pieces of information are passed to `<Greeting />`, with values that aren't strings wrapped in curly braces:
8197
8298
```js
8399
<Greeting name="Frarthur" town="Flundon" age={2} haunted={false} />
84100
```
85101
102+
In functional components, props are directly received as an argument to the component function.
103+
104+
```js
105+
function Greeting({ name, town, age, haunted }) {
106+
return (
107+
<div>
108+
<h1>
109+
Hello, {name} from {town}!
110+
</h1>
111+
<p>Age: {age}</p>
112+
<p>Haunted: {haunted ? 'Yes' : 'No'}</p>
113+
</div>
114+
);
115+
}
116+
```
117+
86118
## Displaying the Props
87119
88-
You will often want a component to display the information that you pass.
120+
A component often needs to display the information that is passed to it.
89121
90-
Here’s how to make a component display passed-in information:
122+
To make a component display passed-in information:
91123
92-
1. Find the component class that is going to receive that information.
93-
2. Include `this.props.name-of-information` in that component class’s render method’s `return` statement.
124+
1. Identify the component that will receive the information.
125+
2. Access the prop directly in the function's parameter and use it inside the JSX.
94126
95127
```js
96128
import React from 'react';
97129
import ReactDOM from 'react-dom';
98130

99-
class Greeting extends React.Component {
100-
render() {
101-
return <h1>Hi there, {this.props.firstName}!</h1>;
102-
}
131+
function Greeting({ firstName }) {
132+
return <h1>Hi there, {firstName}!</h1>;
103133
}
104134

105135
ReactDOM.render(<Greeting firstName="Rybu" />, document.getElementById('app'));

0 commit comments

Comments
 (0)