You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
---------
Copy file name to clipboardExpand all lines: content/react/concepts/props/props.md
+60-30Lines changed: 60 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
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.'
4
4
Subjects:
5
5
- 'Web Development'
6
6
Tags:
@@ -11,95 +11,125 @@ CatalogContent:
11
11
- 'paths/front-end-engineer-career-path'
12
12
---
13
13
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.
15
15
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.
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>;
36
38
}
39
+
40
+
export default ParentComponent;
37
41
```
38
42
39
-
## `this.props`
43
+
## `Props` in Functional Components
40
44
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`.
42
46
43
-
A component’s `props` is an object. It holds information about that component.
47
+
### Class-Based Components
44
48
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
+
classGreetingextendsReact.Component {
51
+
render() {
52
+
// Printing the props object
53
+
console.log(this.props);
54
+
55
+
return<h1>Hello world</h1>;
56
+
}
57
+
}
58
+
```
46
59
47
-
```jsx
48
-
render() {
60
+
### Function-Based Components
61
+
62
+
```js
63
+
functionGreeting(props) {
64
+
functionGreeting(props) {
49
65
// Printing the props object
50
-
console.log(this.props);
66
+
console.log(props);
51
67
52
68
return<h1>Hello world</h1>;
53
69
}
54
70
```
55
71
56
72
## Pass `props` to a Component
57
73
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:
59
75
60
76
```js
61
77
<MyComponent foo="bar"/>
62
78
```
63
79
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:
65
81
66
82
```js
67
83
<Example message="This is some top secret info."/>
68
84
```
69
85
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.
71
87
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.
73
89
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:
75
91
76
92
```js
77
93
<Greeting myInfo={['top', 'secret', 'lol']} />
78
94
```
79
95
80
-
In this next example, we pass several pieces of information to `<Greeting />`. The values that aren’t 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:
0 commit comments