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
## Passing simple Props from Parent to Child — where the prop is not a function
2
-
3
-
Inside the parent component, just do ``<ChildComponent propName={this.props.propName} />`` and then inside the child component just do ``{this.props.propName}``
We need a way for the child component to tell the parent component to update without breaking one-way data flow. Since we are using local state, we need a way for the child component to tell the parent component to call setState. One way to solve this is to create a function in the parent component and add it as a property on the object passed to our render prop. Then whenever the child component needs to update state, it calls this function.
32
4
33
-
classFollowersextendsReact.Component {
34
-
render () {
35
-
var followers =this.props.followerList.map(function(follower, index){
36
-
return (<li key={index}>{follower}</li>);
37
-
});
38
-
39
-
return (
40
-
<div>
41
-
<h5>My followers:</h5>
42
-
<ul>
43
-
{followers}
44
-
</ul>
45
-
</div>
46
-
);
47
-
}
48
-
};
49
-
```
50
-
51
-
## Child to Parent — Use a callback and states
5
+
This function then executes in the parent context and calls setState. Once setState is run, if any state value has changed, those new values propagate down into our render prop and the child component now receives the new value.
52
6
53
7
### For passing from child to parent - pass one callback function from parent to child and then use this passed-down function in the child to send something back to parent.
54
8
55
9
Same tutorial - https://medium.com/@ruthmpardee/passing-data-between-react-components-103ad82ebd17
56
10
57
11
### A> Define a callback in my parent which takes the data I need in as a parameter.
58
12
59
-
### B> Pass that callback as a prop to the child (just like the way in Point no-1 above) - this is it will be a regular prop passing from parent to child.
13
+
### B> Pass that callback as a prop to the child.
60
14
61
15
### C> Call the callback using this.props.[callback] in the child (insert your own name where it says [callback] of course), and pass in the data as the argument.
62
16
63
-
64
17
Here’s what that might look like if I had data in **ToDoItem** (the Child Component) that I need to access in **ToDoList**(The parent Component) : And the data that **ToDoList** will receive from the child **ToDoItem** is given a variable name **listInfo** for example.
@@ -82,7 +35,7 @@ class ToDoList extends React.Component {
82
35
// Now from within ToDoItem (The Child Component) we can pass something to callbackFromParent (the prop that was given the value or assigned the value of the CB function that was defined in the parent ) :
83
36
84
37
// Child component
85
-
classToDoItemextendsReact.Component{
38
+
classChildToDoItemextendsReact.Component{
86
39
someFn= () => {
87
40
// [...somewhere in here I define a variable listInfo which I think will be useful as data in my ToDoList component...]
88
41
this.props.callbackFromParent(listInfo);
@@ -94,12 +47,45 @@ class ToDoItem extends React.Component{
94
47
95
48
```
96
49
50
+
ToDoList will now be able to use listInfo within it’s myCallback function!
51
+
52
+
### But what if I want to use 'listInfo' in a different function within ToDoList, not just in myCallback so I get 'listInfo' as a regular variable in the parent component ? With the above implementation, I would only have access as a parameter passed into that one specific method.
53
+
54
+
#### Easy: set this parameter as a state within ToDoList. You can almost think of it as creating a variable within the scope of ToDoList that all the methods within that component can access. In that case my code defining ToDoList might look something like:
Call the callback (fetchProfileBoundFunction) using this.props.[callback] in the child and pass in the data as the argument.
104
+
So in SearchProfile I do < this.props.fetchProfileBoundFunction(username) >
120
105
121
106
## Even Another Implementation of the above concept in below file -
122
107
@@ -125,92 +110,97 @@ So, I pass this callback function to the child-Component SearchProfile as a prop
125
110
updateSearchTerm() in parent component Items.js - Fundamental explanation why I need it at all - Because, here, my most fundamental need is to change the searchTerm ( the parent state ) to whatever I type. But then, I am updating this searchTerm from the child and passing down 'searchTerm' as a prop from parent to child. And Prop is immutable, so I can not directly change 'searchTerm' in the Filter.js
126
111
So, instead I can give the child a function ( updateSearchTerm() in this file ), that the child can call, and that function can manipulate the state.
127
112
128
-
129
113
```js
130
114
classItemsextendsComponent {
131
-
132
-
constructor(props) {
133
-
super(props);
134
-
this.state= {
135
-
searchTerm:''
136
-
};
137
-
}
138
-
139
-
updateSearchTerm=searchTerm=> {
140
-
this.setState({
141
-
searchTerm
142
-
})
143
-
}
144
-
/* In above, I am using object destructuring syntax. So the single 'searchTerm' is equivalent to doing < searchTerm: searchTerm > Which effectively means tha I am telling setState 'Hey take the searchTerm argument of updateSearchTerm() function and set them to be the value of the key-value pair of state (which is an object and both the key and the value is called 'searchTerm' ).
/* In above, I am using object destructuring syntax. So the single 'searchTerm' is equivalent to doing < searchTerm: searchTerm > Which effectively means tha I am telling setState 'Hey take the searchTerm argument of updateSearchTerm() function and set them to be the value of the key-value pair of state (which is an object and both the key and the value is called 'searchTerm' ).
#### 3> Simple example of passing function (a function that has a setState() it it) from parent to child and invoking it in the child - thereby triggering state change in the parent (e.g. on button-click in the child)
0 commit comments