React pass Array from Parent state to child state
up vote
0
down vote
favorite
I have a parent component holding a state called nodes which is established as an array. I am trying to pass this.state.nodes to a child component and have that component have the array as part of the child component state, but it's not working. Here is the code:
<ChildComponent nodes={this.state.graphNodes}/>
This is in the return statement. After some testing/console logging I know the prop is being passed.
In the child component, I have:
this.state = {
nodes: this.props.nodes,
links: ,
totalNodes: ,
totalLinks:
}
But when I try to reference it or map it or do anything with it in the child component render or return it's telling me it's an empty array.
javascript arrays reactjs
add a comment |
up vote
0
down vote
favorite
I have a parent component holding a state called nodes which is established as an array. I am trying to pass this.state.nodes to a child component and have that component have the array as part of the child component state, but it's not working. Here is the code:
<ChildComponent nodes={this.state.graphNodes}/>
This is in the return statement. After some testing/console logging I know the prop is being passed.
In the child component, I have:
this.state = {
nodes: this.props.nodes,
links: ,
totalNodes: ,
totalLinks:
}
But when I try to reference it or map it or do anything with it in the child component render or return it's telling me it's an empty array.
javascript arrays reactjs
1
Do you callsuper(props);in the child component constructor? It might also be thatthis.state.graphNodesin your parent component is fetched asynchronously, so when you first render the child component it is not set yet.
– Tholle
Nov 9 at 16:19
2
show a more complete example of your ChildComponent code. Also, just usethis.props.nodesin your child render function
– Brandon
Nov 9 at 16:19
...and anyway it's typically bad choice to put things fromproprstostate. better iterate overthis.props.nodesin child'srender()
– skyboyer
Nov 9 at 16:35
Thank you all for the feedback. You were all correct ... this.props.nodes used directly instead of placing it in the child component state worked perfectly.
– CHays412
Nov 9 at 18:47
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a parent component holding a state called nodes which is established as an array. I am trying to pass this.state.nodes to a child component and have that component have the array as part of the child component state, but it's not working. Here is the code:
<ChildComponent nodes={this.state.graphNodes}/>
This is in the return statement. After some testing/console logging I know the prop is being passed.
In the child component, I have:
this.state = {
nodes: this.props.nodes,
links: ,
totalNodes: ,
totalLinks:
}
But when I try to reference it or map it or do anything with it in the child component render or return it's telling me it's an empty array.
javascript arrays reactjs
I have a parent component holding a state called nodes which is established as an array. I am trying to pass this.state.nodes to a child component and have that component have the array as part of the child component state, but it's not working. Here is the code:
<ChildComponent nodes={this.state.graphNodes}/>
This is in the return statement. After some testing/console logging I know the prop is being passed.
In the child component, I have:
this.state = {
nodes: this.props.nodes,
links: ,
totalNodes: ,
totalLinks:
}
But when I try to reference it or map it or do anything with it in the child component render or return it's telling me it's an empty array.
javascript arrays reactjs
javascript arrays reactjs
asked Nov 9 at 16:17
CHays412
405
405
1
Do you callsuper(props);in the child component constructor? It might also be thatthis.state.graphNodesin your parent component is fetched asynchronously, so when you first render the child component it is not set yet.
– Tholle
Nov 9 at 16:19
2
show a more complete example of your ChildComponent code. Also, just usethis.props.nodesin your child render function
– Brandon
Nov 9 at 16:19
...and anyway it's typically bad choice to put things fromproprstostate. better iterate overthis.props.nodesin child'srender()
– skyboyer
Nov 9 at 16:35
Thank you all for the feedback. You were all correct ... this.props.nodes used directly instead of placing it in the child component state worked perfectly.
– CHays412
Nov 9 at 18:47
add a comment |
1
Do you callsuper(props);in the child component constructor? It might also be thatthis.state.graphNodesin your parent component is fetched asynchronously, so when you first render the child component it is not set yet.
– Tholle
Nov 9 at 16:19
2
show a more complete example of your ChildComponent code. Also, just usethis.props.nodesin your child render function
– Brandon
Nov 9 at 16:19
...and anyway it's typically bad choice to put things fromproprstostate. better iterate overthis.props.nodesin child'srender()
– skyboyer
Nov 9 at 16:35
Thank you all for the feedback. You were all correct ... this.props.nodes used directly instead of placing it in the child component state worked perfectly.
– CHays412
Nov 9 at 18:47
1
1
Do you call
super(props); in the child component constructor? It might also be that this.state.graphNodes in your parent component is fetched asynchronously, so when you first render the child component it is not set yet.– Tholle
Nov 9 at 16:19
Do you call
super(props); in the child component constructor? It might also be that this.state.graphNodes in your parent component is fetched asynchronously, so when you first render the child component it is not set yet.– Tholle
Nov 9 at 16:19
2
2
show a more complete example of your ChildComponent code. Also, just use
this.props.nodes in your child render function– Brandon
Nov 9 at 16:19
show a more complete example of your ChildComponent code. Also, just use
this.props.nodes in your child render function– Brandon
Nov 9 at 16:19
...and anyway it's typically bad choice to put things from
proprs to state. better iterate over this.props.nodes in child's render()– skyboyer
Nov 9 at 16:35
...and anyway it's typically bad choice to put things from
proprs to state. better iterate over this.props.nodes in child's render()– skyboyer
Nov 9 at 16:35
Thank you all for the feedback. You were all correct ... this.props.nodes used directly instead of placing it in the child component state worked perfectly.
– CHays412
Nov 9 at 18:47
Thank you all for the feedback. You were all correct ... this.props.nodes used directly instead of placing it in the child component state worked perfectly.
– CHays412
Nov 9 at 18:47
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Since graphNodes is in a component state, i think its initial value should be an empty array, and you are initializing state with props.nodes at constructor, since constructor renders only once at initialization stage of the component at that moment graphNodes must be an empty array, after you change the parent component state, the state in the child component will not update. That makes graphNodes state always an empty array.
Why don't you use props directly, if you don't have to change the graphNodes values in the child component you can use it directly from props.
If you really want map props to state, you have to do it in componentDidUpdate lifecycle method with proper conditions
1
I second the suggestion to use props.nodes directly. Because an array in js is an "object", you will pass the array as a reference instead of as a copy, meaning you will be using the same set of data on both the parent and child component. If the data updates, I would suggest having a function/method passed to the child component to change the state on the parent component, and the changes will also be reflected in the child component via props.nodes
– OzzyTheGiant
Nov 9 at 16:47
Thanks Ozzy! You were correct and I appreciate your help.
– CHays412
Nov 9 at 18:46
add a comment |
up vote
0
down vote
All of the comments were dead on. Using this.props.nodes directly instead of trying to put it in child state was the perfect solution. Thanks to all who replied!
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Since graphNodes is in a component state, i think its initial value should be an empty array, and you are initializing state with props.nodes at constructor, since constructor renders only once at initialization stage of the component at that moment graphNodes must be an empty array, after you change the parent component state, the state in the child component will not update. That makes graphNodes state always an empty array.
Why don't you use props directly, if you don't have to change the graphNodes values in the child component you can use it directly from props.
If you really want map props to state, you have to do it in componentDidUpdate lifecycle method with proper conditions
1
I second the suggestion to use props.nodes directly. Because an array in js is an "object", you will pass the array as a reference instead of as a copy, meaning you will be using the same set of data on both the parent and child component. If the data updates, I would suggest having a function/method passed to the child component to change the state on the parent component, and the changes will also be reflected in the child component via props.nodes
– OzzyTheGiant
Nov 9 at 16:47
Thanks Ozzy! You were correct and I appreciate your help.
– CHays412
Nov 9 at 18:46
add a comment |
up vote
1
down vote
Since graphNodes is in a component state, i think its initial value should be an empty array, and you are initializing state with props.nodes at constructor, since constructor renders only once at initialization stage of the component at that moment graphNodes must be an empty array, after you change the parent component state, the state in the child component will not update. That makes graphNodes state always an empty array.
Why don't you use props directly, if you don't have to change the graphNodes values in the child component you can use it directly from props.
If you really want map props to state, you have to do it in componentDidUpdate lifecycle method with proper conditions
1
I second the suggestion to use props.nodes directly. Because an array in js is an "object", you will pass the array as a reference instead of as a copy, meaning you will be using the same set of data on both the parent and child component. If the data updates, I would suggest having a function/method passed to the child component to change the state on the parent component, and the changes will also be reflected in the child component via props.nodes
– OzzyTheGiant
Nov 9 at 16:47
Thanks Ozzy! You were correct and I appreciate your help.
– CHays412
Nov 9 at 18:46
add a comment |
up vote
1
down vote
up vote
1
down vote
Since graphNodes is in a component state, i think its initial value should be an empty array, and you are initializing state with props.nodes at constructor, since constructor renders only once at initialization stage of the component at that moment graphNodes must be an empty array, after you change the parent component state, the state in the child component will not update. That makes graphNodes state always an empty array.
Why don't you use props directly, if you don't have to change the graphNodes values in the child component you can use it directly from props.
If you really want map props to state, you have to do it in componentDidUpdate lifecycle method with proper conditions
Since graphNodes is in a component state, i think its initial value should be an empty array, and you are initializing state with props.nodes at constructor, since constructor renders only once at initialization stage of the component at that moment graphNodes must be an empty array, after you change the parent component state, the state in the child component will not update. That makes graphNodes state always an empty array.
Why don't you use props directly, if you don't have to change the graphNodes values in the child component you can use it directly from props.
If you really want map props to state, you have to do it in componentDidUpdate lifecycle method with proper conditions
edited Nov 9 at 16:55
answered Nov 9 at 16:41
Hafeez Hamza
307110
307110
1
I second the suggestion to use props.nodes directly. Because an array in js is an "object", you will pass the array as a reference instead of as a copy, meaning you will be using the same set of data on both the parent and child component. If the data updates, I would suggest having a function/method passed to the child component to change the state on the parent component, and the changes will also be reflected in the child component via props.nodes
– OzzyTheGiant
Nov 9 at 16:47
Thanks Ozzy! You were correct and I appreciate your help.
– CHays412
Nov 9 at 18:46
add a comment |
1
I second the suggestion to use props.nodes directly. Because an array in js is an "object", you will pass the array as a reference instead of as a copy, meaning you will be using the same set of data on both the parent and child component. If the data updates, I would suggest having a function/method passed to the child component to change the state on the parent component, and the changes will also be reflected in the child component via props.nodes
– OzzyTheGiant
Nov 9 at 16:47
Thanks Ozzy! You were correct and I appreciate your help.
– CHays412
Nov 9 at 18:46
1
1
I second the suggestion to use props.nodes directly. Because an array in js is an "object", you will pass the array as a reference instead of as a copy, meaning you will be using the same set of data on both the parent and child component. If the data updates, I would suggest having a function/method passed to the child component to change the state on the parent component, and the changes will also be reflected in the child component via props.nodes
– OzzyTheGiant
Nov 9 at 16:47
I second the suggestion to use props.nodes directly. Because an array in js is an "object", you will pass the array as a reference instead of as a copy, meaning you will be using the same set of data on both the parent and child component. If the data updates, I would suggest having a function/method passed to the child component to change the state on the parent component, and the changes will also be reflected in the child component via props.nodes
– OzzyTheGiant
Nov 9 at 16:47
Thanks Ozzy! You were correct and I appreciate your help.
– CHays412
Nov 9 at 18:46
Thanks Ozzy! You were correct and I appreciate your help.
– CHays412
Nov 9 at 18:46
add a comment |
up vote
0
down vote
All of the comments were dead on. Using this.props.nodes directly instead of trying to put it in child state was the perfect solution. Thanks to all who replied!
add a comment |
up vote
0
down vote
All of the comments were dead on. Using this.props.nodes directly instead of trying to put it in child state was the perfect solution. Thanks to all who replied!
add a comment |
up vote
0
down vote
up vote
0
down vote
All of the comments were dead on. Using this.props.nodes directly instead of trying to put it in child state was the perfect solution. Thanks to all who replied!
All of the comments were dead on. Using this.props.nodes directly instead of trying to put it in child state was the perfect solution. Thanks to all who replied!
answered Nov 9 at 18:46
CHays412
405
405
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229477%2freact-pass-array-from-parent-state-to-child-state%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Do you call
super(props);in the child component constructor? It might also be thatthis.state.graphNodesin your parent component is fetched asynchronously, so when you first render the child component it is not set yet.– Tholle
Nov 9 at 16:19
2
show a more complete example of your ChildComponent code. Also, just use
this.props.nodesin your child render function– Brandon
Nov 9 at 16:19
...and anyway it's typically bad choice to put things from
proprstostate. better iterate overthis.props.nodesin child'srender()– skyboyer
Nov 9 at 16:35
Thank you all for the feedback. You were all correct ... this.props.nodes used directly instead of placing it in the child component state worked perfectly.
– CHays412
Nov 9 at 18:47