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
Copy file name to clipboardExpand all lines: src/tutorial/08-ComposingComponents.md
+19-20Lines changed: 19 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,9 @@ class Dialog extends React.Component {
14
14
}
15
15
}
16
16
```
17
-
That sounds simple except we don't know what the content of the dialog is going to be. Imagine at one place we might want to display an employee contact within a dialog and at some other place we might want to display a grid. This component itself has no knowledge of it beforehand. And if we wanted to make this component truly reusable it **should not** have any knowledge of it beforehand.
17
+
That sounds simple except we don't know what the content of the dialog is going to be. Imagine at one place we might want to display an employee contact within a dialog and at some other place we might want to display a grid. This component itself has no knowledge of it beforehand. And if we wanted to make this component truly reusable it **should not** have any knowledge of it beforehand.
18
18
19
-
The job of `Dialog` component is to render a modal window but it shouldn't care about the content - we should be able to use this component at different places to render different things.
19
+
The job of the `Dialog` component is to render a modal window but it shouldn't care about the content - we should be able to use this component at different places to render different things.
20
20
21
21
How do we tell this dialog to display different things?
22
22
@@ -33,13 +33,13 @@ class EmployeeProfileDialog extends React.Component {
33
33
//We have passed a child component inside the Dialog
34
34
return (
35
35
<Dialog>
36
-
<EmployeeProfile></EmployeeProfile>
36
+
<EmployeeProfile/>
37
37
</Dialog>
38
38
)
39
39
}
40
40
}
41
41
```
42
-
Here we have a component called `EmployeeProfile` (any other valid React component would work here) as a "children" of `Dialog`. When we pass a "children" to a component it will be available inside that component as a `props` called `children`. So in this case, `EmployeeProfile` will be available inside the `Dialog` component as `props.children`. We can rewrite render function of `Dialog` so that it renders `props.children`.
42
+
Here we have a component called `EmployeeProfile` (any other valid React component would also work here) as a "child" of `Dialog`. When we pass a "child" to a component it will be available inside that component as a `props` called `children`. So in this case, `EmployeeProfile` will be available inside the `Dialog` component as `props.children`. We can rewrite render function of `Dialog` so that it renders `props.children`.
43
43
44
44
```jsx
45
45
classDialogextendsReact.Component {
@@ -54,30 +54,31 @@ class Dialog extends React.Component {
54
54
}
55
55
}
56
56
```
57
-
Think about what we did here. `Dialog` component really doesn't know what it's `children` would be beforehand. Whoever is using this `Dialog` component can pass in any `children` that they like. Within the `render` function `Dialog` says - hey I'll display anything my user passes me as children, I don't need to know what that is.
57
+
Think about what we did here. The `Dialog` component really doesn't know what it's `children` would be beforehand. Whoever is using this `Dialog` component can pass in any `children` that they like. Within the `render` function `Dialog` says - hey I'll display anything my user passes me as `children`, I don't need to know what that is.
58
58
59
59
This my friend is composition and this is mighty powerful if you want to write reusable components.
60
60
61
61
2. render props
62
62
63
-
Another pattern to achieve similar thing in React is by using what's known as "render props" pattern.
63
+
Another pattern to achieve a similar thing in React is by using what's known as "render props" pattern.
64
64
65
-
So now let's take the same dialog example but let's make it more sophisticated. Imagine the dialog has a header, body and footer section. We want the user of this component to be able display anything they like on those three section.
65
+
So now let's take the same dialog example but let's make it more sophisticated. Imagine the dialog has a header, body and footer section. We want the user of this component to be able to display anything they like within either of those three sections.
66
66
67
67
```jsx
68
68
classDialogextendsReact.Component {
69
69
render(){
70
70
return (
71
71
<div className={'dialog'}>
72
-
<div className={'dialogHeader}>
72
+
<div className={'dialogHeader'}>
73
73
{
74
74
/** user of this component should be able to display any header inside here */
75
75
}
76
76
</div>
77
77
<div className={'dialogBody'}>
78
78
{
79
79
/** user of this component should be able to display any body inside here */
80
-
} </div>
80
+
}
81
+
</div>
81
82
<div className={'dialogFooter'}>
82
83
{
83
84
/** user of this component should be able to display any footer inside here */
@@ -89,9 +90,9 @@ class Dialog extends React.Component {
89
90
}
90
91
```
91
92
92
-
The `props.children` approach we used above might not work well here. All the children we pass as "children" would be available as `props.children` and it might be little cumbersome to split the children into these three segment - "header", "body" and "footer". Wouldn't it be nice if we could tell the `Dialog` component explicitly what it should render on"header", "body" and "footer" section?
93
+
The `props.children` approach we used above might not work well here. All the children we pass as "children" would be available as `props.children` and it might be little cumbersome to split the children into our three segments - "header", "body" and "footer". Wouldn't it be nice if we could tell the `Dialog` component explicitly what it should render within each "header", "body" and "footer" section?
93
94
94
-
We can use the "render props" pattern to do exactly that. Allthis means is that we can pass three render function as `props` that tells what the dialog should render inside each segment. For ex lets look at a usage:
95
+
We can use the "render props" pattern to do exactly that. All this means is that we can pass three render functions as `props` that tells the dialog what it should render inside each segment. For example, lets look at a usage:
@@ -106,19 +107,19 @@ class EmployeeProfileDialog extends React.Component {
106
107
}
107
108
}
108
109
```
109
-
Here all we are really doing is passing three different `props` called `renderHeader`, `renderBody` and `renderFooter`. They are all just a function when called returns appropriate React component to display. Now we can edit the `Dialog` component to get it working as expected:
110
+
Here, all we are really doing is passing three different `props` called `renderHeader`, `renderBody` and `renderFooter`. They are all just functions, and when called will return the appropriate React component to display. Now we can edit the `Dialog` component to get it working as expected:
110
111
111
112
```jsx
112
113
classDialogextendsReact.Component {
113
114
render(){
114
115
return (
115
116
<div className={'dialog'}>
116
-
<div className={'dialogHeader}>
117
+
<div className={'dialogHeader'}>
117
118
{this.props.renderHeader()}
118
119
</div>
119
120
<div className={'dialogBody'}>
120
121
{this.props.renderBody()}
121
-
</div
122
+
</div>
122
123
<div className={'dialogFooter'}>
123
124
{this.props.renderFooter()}
124
125
</div>
@@ -128,18 +129,16 @@ class Dialog extends React.Component {
128
129
}
129
130
```
130
131
131
-
Here instead of displaying `this.props.children` like we did previously, we executed different props function as appropriate on different section. Whatever is returned from that function during runtime will be displayed on that section. The `Dialog` component remains completely agnostic to what it's displaying.
132
+
Here, instead of displaying `this.props.children` like we did previously, we executed the different `props` functions as appropriate on the different sections. Whatever is returned from that function during runtime will be displayed within that section. The `Dialog` component remains completely agnostic to what it's displaying.
132
133
133
-
Let's do some exercise shall we? Please open the exercise file and follow to instructions to make appropriate changes.
134
+
Let's do some exercises shall we? Please open the exercise file and follow the instructions to make the appropriate changes.
134
135
135
136
<!--exercise-->
136
137
137
138
There are three things I would like you to note here:
138
139
139
140
1. We named those props above with `render` prefix (`renderHeader`, `renderFooter` etc) but it's just a naming convention, we could have named it anything. It's just nice to prefix it with `render` because someone looking at our code would easily be able tell that we are using this `props` function to render something.
140
141
141
-
2. The "render" props we passed is just any regular javascript function, its just that it's returning some component. We can do really anything here that we can doin a javascript function. When we called these function inside the `Dialog` we could pass some arguments if we like. The only limit here really is your imagination here.
142
-
143
-
3. We can pass around components in React as a `props` just like we can pass an object or string or function. End of the day React component are just functions. So take the liberty of passing it around.
144
-
142
+
2. The "render" props we passed is just any regular javascript function, its just that it's returning some component. We can do really anything here that we can do in a javascript function. When we called these function inside the `Dialog` we could pass some arguments if we like. The only limit here really is your imagination.
145
143
144
+
3. We can pass around components in React as `props`, just like we can pass an object or string or function. At the end of the day, React components are just functions. So take the liberty of passing it around.
0 commit comments