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.










share|improve this question


















  • 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






  • 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










  • ...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















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.










share|improve this question


















  • 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






  • 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










  • ...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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 16:17









CHays412

405




405








  • 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






  • 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










  • ...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














  • 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






  • 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










  • ...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








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












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






share|improve this answer



















  • 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


















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!






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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






    share|improve this answer



















    • 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















    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






    share|improve this answer



















    • 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













    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






    share|improve this answer














    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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














    • 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












    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!






    share|improve this answer

























      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!






      share|improve this answer























        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!






        share|improve this answer












        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!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 18:46









        CHays412

        405




        405






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Landwehr

            Reims

            Schenkenzell