React/Redux Can't update initial state / undefined problem





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to update my state and getting it to render.



Below I am calling my state from the reducer.js file and displaying the initial list fine.



<div className={classes.resultborder}>
{this.props.transInput.map(data => {
return (
<Inputs
key={data.id}
xParty={data.xParty}
zParty={data.zParty}
yAction={data.yAction}
amount={data.amount}
deleteItem={() => this.deleteItem(data.id)}/>
);
})}
</div>


At the bottom of my parent app, I have



const mapStateToProps = state => {
return {
transInput: state.transactionInputs
};
};

const mapDispatchToProps = dispatch => {
return {
submitResults: () => dispatch({type: 'SUBMIT', x: {
xParty: 'Henry',
yAction: 'Funds',
zParty: 'Elizbath'
}})
};
};

export default connect(mapStateToProps, mapDispatchToProps)(Transactions);


I have hard coded my submitResults because I have not yet been able to update my state and display, this is for simplicity.



I am calling submitResults in a button



submitResults={this.props.submitResults}


Now below is my whole reducer



const initialReducer = {

transactionInputs: [
{id: 1, xParty: "statePaul", yAction: "Funds", zParty: "stateSandra", amount: 100},
{id: 2, xParty: "stateEmily", yAction: "Loans", zParty: "stateJohn", amount: 200},
{id: 3, xParty: "stateMatt", yAction: "Repays", zParty: "stateMicheal", amount: 300},
],
emptyInputs: false,
toggle: false
};

const reducer = (state = initialReducer, action) => {

console.log(initialReducer.transactionInputs);

if(action.type === 'SUBMIT'){
return{
...state,
xParty: state.xParty.concat(action.x)
}
}
return state;
};

export default reducer;


When I press te button to run submitResults I get the following error message TypeError: Cannot read property 'concat' of undefined



I want to be able to update my initial state and have it displayed (currently I do believe if I can get the initial state to update then the display will re-render fine).



I am not completely sure why this is not working but I do believe it's in my reducer.js file has something to dow ith how I'm returning my state.



I apprentice any suggestions.










share|improve this question


















  • 2





    xParty is not in initialReducer but rather it is a key inside of the transactionInputs array element(s). So this would make more sense state.transactionInputs[0].xParty. The error is correct, there is no xParty key in the object initialReducer.

    – Annjawn
    Jan 3 at 21:04











  • Okay, is the error this line then ` xParty: state.xParty.concat(action.x)` ? How would I go about fixing it? I tried ` transactionInputs: state.transactionInputs.concat(action.x)` but it did not work.

    – NoobCoder
    Jan 3 at 21:12











  • Its hard to tell how to fix it without knowing much of your requirements. Based on what you posted action.x doesn't look like an array, it's an object, and transactionInputs is an array of objects. You concat two arrays, but concating an array to an object won't work. Changing action.x to be an array may solve your problem x: [{ xParty: 'Henry', yAction: 'Funds', zParty: 'Elizbath' }]

    – Annjawn
    Jan 3 at 21:23













  • @Annjawn thank you for your help, I have figured out what was wrong and answered my own question but I want to thank you again for your time, you helped guide me in the right direct friend.

    – NoobCoder
    Jan 3 at 22:24


















0















I am trying to update my state and getting it to render.



Below I am calling my state from the reducer.js file and displaying the initial list fine.



<div className={classes.resultborder}>
{this.props.transInput.map(data => {
return (
<Inputs
key={data.id}
xParty={data.xParty}
zParty={data.zParty}
yAction={data.yAction}
amount={data.amount}
deleteItem={() => this.deleteItem(data.id)}/>
);
})}
</div>


At the bottom of my parent app, I have



const mapStateToProps = state => {
return {
transInput: state.transactionInputs
};
};

const mapDispatchToProps = dispatch => {
return {
submitResults: () => dispatch({type: 'SUBMIT', x: {
xParty: 'Henry',
yAction: 'Funds',
zParty: 'Elizbath'
}})
};
};

export default connect(mapStateToProps, mapDispatchToProps)(Transactions);


I have hard coded my submitResults because I have not yet been able to update my state and display, this is for simplicity.



I am calling submitResults in a button



submitResults={this.props.submitResults}


Now below is my whole reducer



const initialReducer = {

transactionInputs: [
{id: 1, xParty: "statePaul", yAction: "Funds", zParty: "stateSandra", amount: 100},
{id: 2, xParty: "stateEmily", yAction: "Loans", zParty: "stateJohn", amount: 200},
{id: 3, xParty: "stateMatt", yAction: "Repays", zParty: "stateMicheal", amount: 300},
],
emptyInputs: false,
toggle: false
};

const reducer = (state = initialReducer, action) => {

console.log(initialReducer.transactionInputs);

if(action.type === 'SUBMIT'){
return{
...state,
xParty: state.xParty.concat(action.x)
}
}
return state;
};

export default reducer;


When I press te button to run submitResults I get the following error message TypeError: Cannot read property 'concat' of undefined



I want to be able to update my initial state and have it displayed (currently I do believe if I can get the initial state to update then the display will re-render fine).



I am not completely sure why this is not working but I do believe it's in my reducer.js file has something to dow ith how I'm returning my state.



I apprentice any suggestions.










share|improve this question


















  • 2





    xParty is not in initialReducer but rather it is a key inside of the transactionInputs array element(s). So this would make more sense state.transactionInputs[0].xParty. The error is correct, there is no xParty key in the object initialReducer.

    – Annjawn
    Jan 3 at 21:04











  • Okay, is the error this line then ` xParty: state.xParty.concat(action.x)` ? How would I go about fixing it? I tried ` transactionInputs: state.transactionInputs.concat(action.x)` but it did not work.

    – NoobCoder
    Jan 3 at 21:12











  • Its hard to tell how to fix it without knowing much of your requirements. Based on what you posted action.x doesn't look like an array, it's an object, and transactionInputs is an array of objects. You concat two arrays, but concating an array to an object won't work. Changing action.x to be an array may solve your problem x: [{ xParty: 'Henry', yAction: 'Funds', zParty: 'Elizbath' }]

    – Annjawn
    Jan 3 at 21:23













  • @Annjawn thank you for your help, I have figured out what was wrong and answered my own question but I want to thank you again for your time, you helped guide me in the right direct friend.

    – NoobCoder
    Jan 3 at 22:24














0












0








0








I am trying to update my state and getting it to render.



Below I am calling my state from the reducer.js file and displaying the initial list fine.



<div className={classes.resultborder}>
{this.props.transInput.map(data => {
return (
<Inputs
key={data.id}
xParty={data.xParty}
zParty={data.zParty}
yAction={data.yAction}
amount={data.amount}
deleteItem={() => this.deleteItem(data.id)}/>
);
})}
</div>


At the bottom of my parent app, I have



const mapStateToProps = state => {
return {
transInput: state.transactionInputs
};
};

const mapDispatchToProps = dispatch => {
return {
submitResults: () => dispatch({type: 'SUBMIT', x: {
xParty: 'Henry',
yAction: 'Funds',
zParty: 'Elizbath'
}})
};
};

export default connect(mapStateToProps, mapDispatchToProps)(Transactions);


I have hard coded my submitResults because I have not yet been able to update my state and display, this is for simplicity.



I am calling submitResults in a button



submitResults={this.props.submitResults}


Now below is my whole reducer



const initialReducer = {

transactionInputs: [
{id: 1, xParty: "statePaul", yAction: "Funds", zParty: "stateSandra", amount: 100},
{id: 2, xParty: "stateEmily", yAction: "Loans", zParty: "stateJohn", amount: 200},
{id: 3, xParty: "stateMatt", yAction: "Repays", zParty: "stateMicheal", amount: 300},
],
emptyInputs: false,
toggle: false
};

const reducer = (state = initialReducer, action) => {

console.log(initialReducer.transactionInputs);

if(action.type === 'SUBMIT'){
return{
...state,
xParty: state.xParty.concat(action.x)
}
}
return state;
};

export default reducer;


When I press te button to run submitResults I get the following error message TypeError: Cannot read property 'concat' of undefined



I want to be able to update my initial state and have it displayed (currently I do believe if I can get the initial state to update then the display will re-render fine).



I am not completely sure why this is not working but I do believe it's in my reducer.js file has something to dow ith how I'm returning my state.



I apprentice any suggestions.










share|improve this question














I am trying to update my state and getting it to render.



Below I am calling my state from the reducer.js file and displaying the initial list fine.



<div className={classes.resultborder}>
{this.props.transInput.map(data => {
return (
<Inputs
key={data.id}
xParty={data.xParty}
zParty={data.zParty}
yAction={data.yAction}
amount={data.amount}
deleteItem={() => this.deleteItem(data.id)}/>
);
})}
</div>


At the bottom of my parent app, I have



const mapStateToProps = state => {
return {
transInput: state.transactionInputs
};
};

const mapDispatchToProps = dispatch => {
return {
submitResults: () => dispatch({type: 'SUBMIT', x: {
xParty: 'Henry',
yAction: 'Funds',
zParty: 'Elizbath'
}})
};
};

export default connect(mapStateToProps, mapDispatchToProps)(Transactions);


I have hard coded my submitResults because I have not yet been able to update my state and display, this is for simplicity.



I am calling submitResults in a button



submitResults={this.props.submitResults}


Now below is my whole reducer



const initialReducer = {

transactionInputs: [
{id: 1, xParty: "statePaul", yAction: "Funds", zParty: "stateSandra", amount: 100},
{id: 2, xParty: "stateEmily", yAction: "Loans", zParty: "stateJohn", amount: 200},
{id: 3, xParty: "stateMatt", yAction: "Repays", zParty: "stateMicheal", amount: 300},
],
emptyInputs: false,
toggle: false
};

const reducer = (state = initialReducer, action) => {

console.log(initialReducer.transactionInputs);

if(action.type === 'SUBMIT'){
return{
...state,
xParty: state.xParty.concat(action.x)
}
}
return state;
};

export default reducer;


When I press te button to run submitResults I get the following error message TypeError: Cannot read property 'concat' of undefined



I want to be able to update my initial state and have it displayed (currently I do believe if I can get the initial state to update then the display will re-render fine).



I am not completely sure why this is not working but I do believe it's in my reducer.js file has something to dow ith how I'm returning my state.



I apprentice any suggestions.







reactjs react-redux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 20:58









NoobCoderNoobCoder

216




216








  • 2





    xParty is not in initialReducer but rather it is a key inside of the transactionInputs array element(s). So this would make more sense state.transactionInputs[0].xParty. The error is correct, there is no xParty key in the object initialReducer.

    – Annjawn
    Jan 3 at 21:04











  • Okay, is the error this line then ` xParty: state.xParty.concat(action.x)` ? How would I go about fixing it? I tried ` transactionInputs: state.transactionInputs.concat(action.x)` but it did not work.

    – NoobCoder
    Jan 3 at 21:12











  • Its hard to tell how to fix it without knowing much of your requirements. Based on what you posted action.x doesn't look like an array, it's an object, and transactionInputs is an array of objects. You concat two arrays, but concating an array to an object won't work. Changing action.x to be an array may solve your problem x: [{ xParty: 'Henry', yAction: 'Funds', zParty: 'Elizbath' }]

    – Annjawn
    Jan 3 at 21:23













  • @Annjawn thank you for your help, I have figured out what was wrong and answered my own question but I want to thank you again for your time, you helped guide me in the right direct friend.

    – NoobCoder
    Jan 3 at 22:24














  • 2





    xParty is not in initialReducer but rather it is a key inside of the transactionInputs array element(s). So this would make more sense state.transactionInputs[0].xParty. The error is correct, there is no xParty key in the object initialReducer.

    – Annjawn
    Jan 3 at 21:04











  • Okay, is the error this line then ` xParty: state.xParty.concat(action.x)` ? How would I go about fixing it? I tried ` transactionInputs: state.transactionInputs.concat(action.x)` but it did not work.

    – NoobCoder
    Jan 3 at 21:12











  • Its hard to tell how to fix it without knowing much of your requirements. Based on what you posted action.x doesn't look like an array, it's an object, and transactionInputs is an array of objects. You concat two arrays, but concating an array to an object won't work. Changing action.x to be an array may solve your problem x: [{ xParty: 'Henry', yAction: 'Funds', zParty: 'Elizbath' }]

    – Annjawn
    Jan 3 at 21:23













  • @Annjawn thank you for your help, I have figured out what was wrong and answered my own question but I want to thank you again for your time, you helped guide me in the right direct friend.

    – NoobCoder
    Jan 3 at 22:24








2




2





xParty is not in initialReducer but rather it is a key inside of the transactionInputs array element(s). So this would make more sense state.transactionInputs[0].xParty. The error is correct, there is no xParty key in the object initialReducer.

– Annjawn
Jan 3 at 21:04





xParty is not in initialReducer but rather it is a key inside of the transactionInputs array element(s). So this would make more sense state.transactionInputs[0].xParty. The error is correct, there is no xParty key in the object initialReducer.

– Annjawn
Jan 3 at 21:04













Okay, is the error this line then ` xParty: state.xParty.concat(action.x)` ? How would I go about fixing it? I tried ` transactionInputs: state.transactionInputs.concat(action.x)` but it did not work.

– NoobCoder
Jan 3 at 21:12





Okay, is the error this line then ` xParty: state.xParty.concat(action.x)` ? How would I go about fixing it? I tried ` transactionInputs: state.transactionInputs.concat(action.x)` but it did not work.

– NoobCoder
Jan 3 at 21:12













Its hard to tell how to fix it without knowing much of your requirements. Based on what you posted action.x doesn't look like an array, it's an object, and transactionInputs is an array of objects. You concat two arrays, but concating an array to an object won't work. Changing action.x to be an array may solve your problem x: [{ xParty: 'Henry', yAction: 'Funds', zParty: 'Elizbath' }]

– Annjawn
Jan 3 at 21:23







Its hard to tell how to fix it without knowing much of your requirements. Based on what you posted action.x doesn't look like an array, it's an object, and transactionInputs is an array of objects. You concat two arrays, but concating an array to an object won't work. Changing action.x to be an array may solve your problem x: [{ xParty: 'Henry', yAction: 'Funds', zParty: 'Elizbath' }]

– Annjawn
Jan 3 at 21:23















@Annjawn thank you for your help, I have figured out what was wrong and answered my own question but I want to thank you again for your time, you helped guide me in the right direct friend.

– NoobCoder
Jan 3 at 22:24





@Annjawn thank you for your help, I have figured out what was wrong and answered my own question but I want to thank you again for your time, you helped guide me in the right direct friend.

– NoobCoder
Jan 3 at 22:24












1 Answer
1






active

oldest

votes


















0














In the reducer.js file, under the action type of 'SUBMIT', I had the following code:



 if(action.type === 'SUBMIT'){
return{
...state,
xParty: state.xParty.concat(action.x)
}
}
return state;


But I have that the bug was with the line ...state, and now I have ...state.transactionInputs,



if(action.type === 'SUBMIT'){
return{
...state.transactionInputs,
transactionInputs: state.transactionInputs.concat(action.x)
};
}


SIDE NOTE



When I was trying to figure out what was wrong, I used console.log(initialReducer.transactionInputs) to see if the state was updating but I reliased that was I getting thr initial state and not the current one which was state. So for the console.log I should of used console.log(state.transactionInputs);, I would have solved this much sooner.



Just a side note :)






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',
    autoActivateHeartbeat: false,
    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%2f54029708%2freact-redux-cant-update-initial-state-undefined-problem%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In the reducer.js file, under the action type of 'SUBMIT', I had the following code:



     if(action.type === 'SUBMIT'){
    return{
    ...state,
    xParty: state.xParty.concat(action.x)
    }
    }
    return state;


    But I have that the bug was with the line ...state, and now I have ...state.transactionInputs,



    if(action.type === 'SUBMIT'){
    return{
    ...state.transactionInputs,
    transactionInputs: state.transactionInputs.concat(action.x)
    };
    }


    SIDE NOTE



    When I was trying to figure out what was wrong, I used console.log(initialReducer.transactionInputs) to see if the state was updating but I reliased that was I getting thr initial state and not the current one which was state. So for the console.log I should of used console.log(state.transactionInputs);, I would have solved this much sooner.



    Just a side note :)






    share|improve this answer




























      0














      In the reducer.js file, under the action type of 'SUBMIT', I had the following code:



       if(action.type === 'SUBMIT'){
      return{
      ...state,
      xParty: state.xParty.concat(action.x)
      }
      }
      return state;


      But I have that the bug was with the line ...state, and now I have ...state.transactionInputs,



      if(action.type === 'SUBMIT'){
      return{
      ...state.transactionInputs,
      transactionInputs: state.transactionInputs.concat(action.x)
      };
      }


      SIDE NOTE



      When I was trying to figure out what was wrong, I used console.log(initialReducer.transactionInputs) to see if the state was updating but I reliased that was I getting thr initial state and not the current one which was state. So for the console.log I should of used console.log(state.transactionInputs);, I would have solved this much sooner.



      Just a side note :)






      share|improve this answer


























        0












        0








        0







        In the reducer.js file, under the action type of 'SUBMIT', I had the following code:



         if(action.type === 'SUBMIT'){
        return{
        ...state,
        xParty: state.xParty.concat(action.x)
        }
        }
        return state;


        But I have that the bug was with the line ...state, and now I have ...state.transactionInputs,



        if(action.type === 'SUBMIT'){
        return{
        ...state.transactionInputs,
        transactionInputs: state.transactionInputs.concat(action.x)
        };
        }


        SIDE NOTE



        When I was trying to figure out what was wrong, I used console.log(initialReducer.transactionInputs) to see if the state was updating but I reliased that was I getting thr initial state and not the current one which was state. So for the console.log I should of used console.log(state.transactionInputs);, I would have solved this much sooner.



        Just a side note :)






        share|improve this answer













        In the reducer.js file, under the action type of 'SUBMIT', I had the following code:



         if(action.type === 'SUBMIT'){
        return{
        ...state,
        xParty: state.xParty.concat(action.x)
        }
        }
        return state;


        But I have that the bug was with the line ...state, and now I have ...state.transactionInputs,



        if(action.type === 'SUBMIT'){
        return{
        ...state.transactionInputs,
        transactionInputs: state.transactionInputs.concat(action.x)
        };
        }


        SIDE NOTE



        When I was trying to figure out what was wrong, I used console.log(initialReducer.transactionInputs) to see if the state was updating but I reliased that was I getting thr initial state and not the current one which was state. So for the console.log I should of used console.log(state.transactionInputs);, I would have solved this much sooner.



        Just a side note :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 22:23









        NoobCoderNoobCoder

        216




        216
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029708%2freact-redux-cant-update-initial-state-undefined-problem%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas