Redux reducer with object.assign creates key inside a top key with the same name











up vote
0
down vote

favorite
1












I'm trying to move from the simple example I've learnt on Medium about Redux to properly modify immutable state with object.assign or the spread operator. However, after I've tried this it creates a key in the original state key with the same name. F.e. I had a state key searchPage: 1 and after converting the reducer I've got searchPage: {searchPage: 1}. I bet it's silly, but I did it according to the Redux docs (I only assume). I've tried object.assign and the spread operator, which have the same results. Here is my old reducer:



export function searchPage(state = 1, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return action.searchPage

default:
return state
}
}


and the new with spread operator:



export function searchPage(state = 1, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage }

default:
return state
}
}


update Now, I'm using an initialState object to set initial state for all reducers to an object as a default. However, the spread operator syntax now does exactly the same as before, i.e. inserts the initial state key inside the searchPage key, so I still end up with object inside my searchPage key instead of a number. Here's the updated code, I have no idea if I go in the right direction:



const initialState = {
posts: ,
postsHasErrored: false,
postsIsLoading: false,
searchString: '',
searchCategories: ,
searchPage: 10
}

export function searchString(state = initialState.searchString, action) {
console.log(state)
switch (action.type) {
case 'SET_SEARCH_STRING':
return action.searchString

default:
return state
}
}

export function searchCategories(state = initialState.searchCategories, action) {
switch (action.type) {
case 'SET_SEARCH_CATEGORIES':
return action.searchCategories

default:
return state
}
}

export function searchPage(state = initialState.searchPage, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage }

default:
return state
}
}









share|improve this question
























  • Your old and new reducers implement different behaviors. In first case, all state is simple number, so you replace it to new number. In second case, you always create new object by Object.assign, to which your spread operator is transpiled.
    – Vladislav Ihost
    Jul 24 '17 at 7:26










  • How do I get over it then? Should I leave it as is or change my initial state for the whole application? The problem I'm having is that my React app doesn't re-render after f.e. searchPage is updated in the state, so I thought it's because of the way I'm modifying state. Here's more code of my app stackoverflow.com/questions/45256875/…
    – Mateusz Mysiak
    Jul 24 '17 at 7:29















up vote
0
down vote

favorite
1












I'm trying to move from the simple example I've learnt on Medium about Redux to properly modify immutable state with object.assign or the spread operator. However, after I've tried this it creates a key in the original state key with the same name. F.e. I had a state key searchPage: 1 and after converting the reducer I've got searchPage: {searchPage: 1}. I bet it's silly, but I did it according to the Redux docs (I only assume). I've tried object.assign and the spread operator, which have the same results. Here is my old reducer:



export function searchPage(state = 1, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return action.searchPage

default:
return state
}
}


and the new with spread operator:



export function searchPage(state = 1, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage }

default:
return state
}
}


update Now, I'm using an initialState object to set initial state for all reducers to an object as a default. However, the spread operator syntax now does exactly the same as before, i.e. inserts the initial state key inside the searchPage key, so I still end up with object inside my searchPage key instead of a number. Here's the updated code, I have no idea if I go in the right direction:



const initialState = {
posts: ,
postsHasErrored: false,
postsIsLoading: false,
searchString: '',
searchCategories: ,
searchPage: 10
}

export function searchString(state = initialState.searchString, action) {
console.log(state)
switch (action.type) {
case 'SET_SEARCH_STRING':
return action.searchString

default:
return state
}
}

export function searchCategories(state = initialState.searchCategories, action) {
switch (action.type) {
case 'SET_SEARCH_CATEGORIES':
return action.searchCategories

default:
return state
}
}

export function searchPage(state = initialState.searchPage, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage }

default:
return state
}
}









share|improve this question
























  • Your old and new reducers implement different behaviors. In first case, all state is simple number, so you replace it to new number. In second case, you always create new object by Object.assign, to which your spread operator is transpiled.
    – Vladislav Ihost
    Jul 24 '17 at 7:26










  • How do I get over it then? Should I leave it as is or change my initial state for the whole application? The problem I'm having is that my React app doesn't re-render after f.e. searchPage is updated in the state, so I thought it's because of the way I'm modifying state. Here's more code of my app stackoverflow.com/questions/45256875/…
    – Mateusz Mysiak
    Jul 24 '17 at 7:29













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I'm trying to move from the simple example I've learnt on Medium about Redux to properly modify immutable state with object.assign or the spread operator. However, after I've tried this it creates a key in the original state key with the same name. F.e. I had a state key searchPage: 1 and after converting the reducer I've got searchPage: {searchPage: 1}. I bet it's silly, but I did it according to the Redux docs (I only assume). I've tried object.assign and the spread operator, which have the same results. Here is my old reducer:



export function searchPage(state = 1, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return action.searchPage

default:
return state
}
}


and the new with spread operator:



export function searchPage(state = 1, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage }

default:
return state
}
}


update Now, I'm using an initialState object to set initial state for all reducers to an object as a default. However, the spread operator syntax now does exactly the same as before, i.e. inserts the initial state key inside the searchPage key, so I still end up with object inside my searchPage key instead of a number. Here's the updated code, I have no idea if I go in the right direction:



const initialState = {
posts: ,
postsHasErrored: false,
postsIsLoading: false,
searchString: '',
searchCategories: ,
searchPage: 10
}

export function searchString(state = initialState.searchString, action) {
console.log(state)
switch (action.type) {
case 'SET_SEARCH_STRING':
return action.searchString

default:
return state
}
}

export function searchCategories(state = initialState.searchCategories, action) {
switch (action.type) {
case 'SET_SEARCH_CATEGORIES':
return action.searchCategories

default:
return state
}
}

export function searchPage(state = initialState.searchPage, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage }

default:
return state
}
}









share|improve this question















I'm trying to move from the simple example I've learnt on Medium about Redux to properly modify immutable state with object.assign or the spread operator. However, after I've tried this it creates a key in the original state key with the same name. F.e. I had a state key searchPage: 1 and after converting the reducer I've got searchPage: {searchPage: 1}. I bet it's silly, but I did it according to the Redux docs (I only assume). I've tried object.assign and the spread operator, which have the same results. Here is my old reducer:



export function searchPage(state = 1, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return action.searchPage

default:
return state
}
}


and the new with spread operator:



export function searchPage(state = 1, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage }

default:
return state
}
}


update Now, I'm using an initialState object to set initial state for all reducers to an object as a default. However, the spread operator syntax now does exactly the same as before, i.e. inserts the initial state key inside the searchPage key, so I still end up with object inside my searchPage key instead of a number. Here's the updated code, I have no idea if I go in the right direction:



const initialState = {
posts: ,
postsHasErrored: false,
postsIsLoading: false,
searchString: '',
searchCategories: ,
searchPage: 10
}

export function searchString(state = initialState.searchString, action) {
console.log(state)
switch (action.type) {
case 'SET_SEARCH_STRING':
return action.searchString

default:
return state
}
}

export function searchCategories(state = initialState.searchCategories, action) {
switch (action.type) {
case 'SET_SEARCH_CATEGORIES':
return action.searchCategories

default:
return state
}
}

export function searchPage(state = initialState.searchPage, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage }

default:
return state
}
}






reactjs redux state immutability spread-syntax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 24 '17 at 8:58

























asked Jul 24 '17 at 7:13









Mateusz Mysiak

183215




183215












  • Your old and new reducers implement different behaviors. In first case, all state is simple number, so you replace it to new number. In second case, you always create new object by Object.assign, to which your spread operator is transpiled.
    – Vladislav Ihost
    Jul 24 '17 at 7:26










  • How do I get over it then? Should I leave it as is or change my initial state for the whole application? The problem I'm having is that my React app doesn't re-render after f.e. searchPage is updated in the state, so I thought it's because of the way I'm modifying state. Here's more code of my app stackoverflow.com/questions/45256875/…
    – Mateusz Mysiak
    Jul 24 '17 at 7:29


















  • Your old and new reducers implement different behaviors. In first case, all state is simple number, so you replace it to new number. In second case, you always create new object by Object.assign, to which your spread operator is transpiled.
    – Vladislav Ihost
    Jul 24 '17 at 7:26










  • How do I get over it then? Should I leave it as is or change my initial state for the whole application? The problem I'm having is that my React app doesn't re-render after f.e. searchPage is updated in the state, so I thought it's because of the way I'm modifying state. Here's more code of my app stackoverflow.com/questions/45256875/…
    – Mateusz Mysiak
    Jul 24 '17 at 7:29
















Your old and new reducers implement different behaviors. In first case, all state is simple number, so you replace it to new number. In second case, you always create new object by Object.assign, to which your spread operator is transpiled.
– Vladislav Ihost
Jul 24 '17 at 7:26




Your old and new reducers implement different behaviors. In first case, all state is simple number, so you replace it to new number. In second case, you always create new object by Object.assign, to which your spread operator is transpiled.
– Vladislav Ihost
Jul 24 '17 at 7:26












How do I get over it then? Should I leave it as is or change my initial state for the whole application? The problem I'm having is that my React app doesn't re-render after f.e. searchPage is updated in the state, so I thought it's because of the way I'm modifying state. Here's more code of my app stackoverflow.com/questions/45256875/…
– Mateusz Mysiak
Jul 24 '17 at 7:29




How do I get over it then? Should I leave it as is or change my initial state for the whole application? The problem I'm having is that my React app doesn't re-render after f.e. searchPage is updated in the state, so I thought it's because of the way I'm modifying state. Here's more code of my app stackoverflow.com/questions/45256875/…
– Mateusz Mysiak
Jul 24 '17 at 7:29












2 Answers
2






active

oldest

votes

















up vote
0
down vote













It depends on what your action.searchPage value. I think it's an object. Once go through debugging for action object.



try like this



export function searchPage(state = {}, action) {
switch (action.type) {
case 'SET_SEARCH_PAGE':
return { ...state, searchPage: action.searchPage };

default:
return state
}
}





share|improve this answer























  • action.searchPage is always an int number.
    – Mateusz Mysiak
    Jul 24 '17 at 7:18










  • @MateuszMysiak Are you sure for that? If so initialise your state with an empty object as in updated answer.
    – Jyothi Babu Araja
    Jul 24 '17 at 7:19










  • I've done that. Still the same result. In my app state there is searchPage: {searchPage: 1} instead of searchPage: 1
    – Mateusz Mysiak
    Jul 24 '17 at 7:21


















up vote
0
down vote













I had this issue as well.
By researching online I've seen people saying you just needed to initialize your state empty in actions, and while that helped me while I was using only one item from state, it went downhill when I needed two.



This new error happened at my own component file, at mapStateToProps.
Because this works...



const mapStateToProps = state => (
state.posts
);


But this doesn't...



const mapStateToProps = state => (
state.posts, state.comments
);


So I'd end up writing...



const mapStateToProps = state => ({
posts: state.posts,
comments: state.comments
});


Which would duplicate the key name, causing the arrays to be posts.posts and comments.comments. After trying many different syntaxes, I finally found what works:



function mapStateToProps(state){
const { posts } = state.posts
const { comments } = state.comments
return { posts, comments}
}


Hope it helps someone!






share|improve this answer








New contributor




andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    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%2f45274327%2fredux-reducer-with-object-assign-creates-key-inside-a-top-key-with-the-same-name%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    It depends on what your action.searchPage value. I think it's an object. Once go through debugging for action object.



    try like this



    export function searchPage(state = {}, action) {
    switch (action.type) {
    case 'SET_SEARCH_PAGE':
    return { ...state, searchPage: action.searchPage };

    default:
    return state
    }
    }





    share|improve this answer























    • action.searchPage is always an int number.
      – Mateusz Mysiak
      Jul 24 '17 at 7:18










    • @MateuszMysiak Are you sure for that? If so initialise your state with an empty object as in updated answer.
      – Jyothi Babu Araja
      Jul 24 '17 at 7:19










    • I've done that. Still the same result. In my app state there is searchPage: {searchPage: 1} instead of searchPage: 1
      – Mateusz Mysiak
      Jul 24 '17 at 7:21















    up vote
    0
    down vote













    It depends on what your action.searchPage value. I think it's an object. Once go through debugging for action object.



    try like this



    export function searchPage(state = {}, action) {
    switch (action.type) {
    case 'SET_SEARCH_PAGE':
    return { ...state, searchPage: action.searchPage };

    default:
    return state
    }
    }





    share|improve this answer























    • action.searchPage is always an int number.
      – Mateusz Mysiak
      Jul 24 '17 at 7:18










    • @MateuszMysiak Are you sure for that? If so initialise your state with an empty object as in updated answer.
      – Jyothi Babu Araja
      Jul 24 '17 at 7:19










    • I've done that. Still the same result. In my app state there is searchPage: {searchPage: 1} instead of searchPage: 1
      – Mateusz Mysiak
      Jul 24 '17 at 7:21













    up vote
    0
    down vote










    up vote
    0
    down vote









    It depends on what your action.searchPage value. I think it's an object. Once go through debugging for action object.



    try like this



    export function searchPage(state = {}, action) {
    switch (action.type) {
    case 'SET_SEARCH_PAGE':
    return { ...state, searchPage: action.searchPage };

    default:
    return state
    }
    }





    share|improve this answer














    It depends on what your action.searchPage value. I think it's an object. Once go through debugging for action object.



    try like this



    export function searchPage(state = {}, action) {
    switch (action.type) {
    case 'SET_SEARCH_PAGE':
    return { ...state, searchPage: action.searchPage };

    default:
    return state
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 24 '17 at 7:20

























    answered Jul 24 '17 at 7:16









    Jyothi Babu Araja

    5,14321628




    5,14321628












    • action.searchPage is always an int number.
      – Mateusz Mysiak
      Jul 24 '17 at 7:18










    • @MateuszMysiak Are you sure for that? If so initialise your state with an empty object as in updated answer.
      – Jyothi Babu Araja
      Jul 24 '17 at 7:19










    • I've done that. Still the same result. In my app state there is searchPage: {searchPage: 1} instead of searchPage: 1
      – Mateusz Mysiak
      Jul 24 '17 at 7:21


















    • action.searchPage is always an int number.
      – Mateusz Mysiak
      Jul 24 '17 at 7:18










    • @MateuszMysiak Are you sure for that? If so initialise your state with an empty object as in updated answer.
      – Jyothi Babu Araja
      Jul 24 '17 at 7:19










    • I've done that. Still the same result. In my app state there is searchPage: {searchPage: 1} instead of searchPage: 1
      – Mateusz Mysiak
      Jul 24 '17 at 7:21
















    action.searchPage is always an int number.
    – Mateusz Mysiak
    Jul 24 '17 at 7:18




    action.searchPage is always an int number.
    – Mateusz Mysiak
    Jul 24 '17 at 7:18












    @MateuszMysiak Are you sure for that? If so initialise your state with an empty object as in updated answer.
    – Jyothi Babu Araja
    Jul 24 '17 at 7:19




    @MateuszMysiak Are you sure for that? If so initialise your state with an empty object as in updated answer.
    – Jyothi Babu Araja
    Jul 24 '17 at 7:19












    I've done that. Still the same result. In my app state there is searchPage: {searchPage: 1} instead of searchPage: 1
    – Mateusz Mysiak
    Jul 24 '17 at 7:21




    I've done that. Still the same result. In my app state there is searchPage: {searchPage: 1} instead of searchPage: 1
    – Mateusz Mysiak
    Jul 24 '17 at 7:21












    up vote
    0
    down vote













    I had this issue as well.
    By researching online I've seen people saying you just needed to initialize your state empty in actions, and while that helped me while I was using only one item from state, it went downhill when I needed two.



    This new error happened at my own component file, at mapStateToProps.
    Because this works...



    const mapStateToProps = state => (
    state.posts
    );


    But this doesn't...



    const mapStateToProps = state => (
    state.posts, state.comments
    );


    So I'd end up writing...



    const mapStateToProps = state => ({
    posts: state.posts,
    comments: state.comments
    });


    Which would duplicate the key name, causing the arrays to be posts.posts and comments.comments. After trying many different syntaxes, I finally found what works:



    function mapStateToProps(state){
    const { posts } = state.posts
    const { comments } = state.comments
    return { posts, comments}
    }


    Hope it helps someone!






    share|improve this answer








    New contributor




    andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













      I had this issue as well.
      By researching online I've seen people saying you just needed to initialize your state empty in actions, and while that helped me while I was using only one item from state, it went downhill when I needed two.



      This new error happened at my own component file, at mapStateToProps.
      Because this works...



      const mapStateToProps = state => (
      state.posts
      );


      But this doesn't...



      const mapStateToProps = state => (
      state.posts, state.comments
      );


      So I'd end up writing...



      const mapStateToProps = state => ({
      posts: state.posts,
      comments: state.comments
      });


      Which would duplicate the key name, causing the arrays to be posts.posts and comments.comments. After trying many different syntaxes, I finally found what works:



      function mapStateToProps(state){
      const { posts } = state.posts
      const { comments } = state.comments
      return { posts, comments}
      }


      Hope it helps someone!






      share|improve this answer








      New contributor




      andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









        I had this issue as well.
        By researching online I've seen people saying you just needed to initialize your state empty in actions, and while that helped me while I was using only one item from state, it went downhill when I needed two.



        This new error happened at my own component file, at mapStateToProps.
        Because this works...



        const mapStateToProps = state => (
        state.posts
        );


        But this doesn't...



        const mapStateToProps = state => (
        state.posts, state.comments
        );


        So I'd end up writing...



        const mapStateToProps = state => ({
        posts: state.posts,
        comments: state.comments
        });


        Which would duplicate the key name, causing the arrays to be posts.posts and comments.comments. After trying many different syntaxes, I finally found what works:



        function mapStateToProps(state){
        const { posts } = state.posts
        const { comments } = state.comments
        return { posts, comments}
        }


        Hope it helps someone!






        share|improve this answer








        New contributor




        andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        I had this issue as well.
        By researching online I've seen people saying you just needed to initialize your state empty in actions, and while that helped me while I was using only one item from state, it went downhill when I needed two.



        This new error happened at my own component file, at mapStateToProps.
        Because this works...



        const mapStateToProps = state => (
        state.posts
        );


        But this doesn't...



        const mapStateToProps = state => (
        state.posts, state.comments
        );


        So I'd end up writing...



        const mapStateToProps = state => ({
        posts: state.posts,
        comments: state.comments
        });


        Which would duplicate the key name, causing the arrays to be posts.posts and comments.comments. After trying many different syntaxes, I finally found what works:



        function mapStateToProps(state){
        const { posts } = state.posts
        const { comments } = state.comments
        return { posts, comments}
        }


        Hope it helps someone!







        share|improve this answer








        New contributor




        andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered yesterday









        andreaweb

        12




        12




        New contributor




        andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        andreaweb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45274327%2fredux-reducer-with-object-assign-creates-key-inside-a-top-key-with-the-same-name%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python