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

Multi tool use
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
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 byObject.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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
reactjs redux state immutability spread-syntax
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 byObject.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
add a comment |
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 byObject.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
add a comment |
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
}
}
action.searchPage is always an int number.
– Mateusz Mysiak
Jul 24 '17 at 7:18
@MateuszMysiak Are you sure for that? If so initialise yourstate
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 issearchPage: {searchPage: 1}
instead ofsearchPage: 1
– Mateusz Mysiak
Jul 24 '17 at 7:21
add a comment |
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!
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.
add a comment |
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
}
}
action.searchPage is always an int number.
– Mateusz Mysiak
Jul 24 '17 at 7:18
@MateuszMysiak Are you sure for that? If so initialise yourstate
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 issearchPage: {searchPage: 1}
instead ofsearchPage: 1
– Mateusz Mysiak
Jul 24 '17 at 7:21
add a comment |
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
}
}
action.searchPage is always an int number.
– Mateusz Mysiak
Jul 24 '17 at 7:18
@MateuszMysiak Are you sure for that? If so initialise yourstate
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 issearchPage: {searchPage: 1}
instead ofsearchPage: 1
– Mateusz Mysiak
Jul 24 '17 at 7:21
add a comment |
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
}
}
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
}
}
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 yourstate
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 issearchPage: {searchPage: 1}
instead ofsearchPage: 1
– Mateusz Mysiak
Jul 24 '17 at 7:21
add a comment |
action.searchPage is always an int number.
– Mateusz Mysiak
Jul 24 '17 at 7:18
@MateuszMysiak Are you sure for that? If so initialise yourstate
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 issearchPage: {searchPage: 1}
instead ofsearchPage: 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
add a comment |
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!
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.
add a comment |
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!
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.
add a comment |
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!
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!
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.
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.
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
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
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
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
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
jS A7wYuX9vrI,PF33pwf2xtpV7 YKqU3Sg2aXJB69ODHtM,rEMm,rxPy,fo50YuA8SIv1wAp
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