Using spread to add an object that I get from API into my initial datasource












1















I do have an initial datasource:



const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};


Once application loads (this is a chrome extension I am building for myself), I am getting opened tabs from chrome and would like to add them into this datasource as a new column.



Here's my attempt to do it:



export default (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: [{"chromeTabs"}, ...state.columnOrder]
};

default:
return state;
}
};


I expect to create a data object like below by above function:



const state = {
columns: [
{
id: 'chromeTabs,
title: 'chromeTabs',
tabs:
[tab1,tab2,tab3,tab4]
},
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['chromeTabs', 'column-2']
}
};


Unfortunately this has not worked for me. I would appreciate any directions.










share|improve this question




















  • 1





    What are you expecting the resulting redux state to look like?

    – Alexander Nied
    Nov 21 '18 at 17:39






  • 1





    "Unfortunately this has not worked for me..." In what way? Always quote any error message, etc., you're getting, or what behavior you're seeing that doesn't match what you expect.

    – T.J. Crowder
    Nov 21 '18 at 17:39











  • Also, I don't believe {"chromeTabs"} is a valid syntax of any kind.

    – Alexander Nied
    Nov 21 '18 at 17:40
















1















I do have an initial datasource:



const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};


Once application loads (this is a chrome extension I am building for myself), I am getting opened tabs from chrome and would like to add them into this datasource as a new column.



Here's my attempt to do it:



export default (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: [{"chromeTabs"}, ...state.columnOrder]
};

default:
return state;
}
};


I expect to create a data object like below by above function:



const state = {
columns: [
{
id: 'chromeTabs,
title: 'chromeTabs',
tabs:
[tab1,tab2,tab3,tab4]
},
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['chromeTabs', 'column-2']
}
};


Unfortunately this has not worked for me. I would appreciate any directions.










share|improve this question




















  • 1





    What are you expecting the resulting redux state to look like?

    – Alexander Nied
    Nov 21 '18 at 17:39






  • 1





    "Unfortunately this has not worked for me..." In what way? Always quote any error message, etc., you're getting, or what behavior you're seeing that doesn't match what you expect.

    – T.J. Crowder
    Nov 21 '18 at 17:39











  • Also, I don't believe {"chromeTabs"} is a valid syntax of any kind.

    – Alexander Nied
    Nov 21 '18 at 17:40














1












1








1








I do have an initial datasource:



const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};


Once application loads (this is a chrome extension I am building for myself), I am getting opened tabs from chrome and would like to add them into this datasource as a new column.



Here's my attempt to do it:



export default (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: [{"chromeTabs"}, ...state.columnOrder]
};

default:
return state;
}
};


I expect to create a data object like below by above function:



const state = {
columns: [
{
id: 'chromeTabs,
title: 'chromeTabs',
tabs:
[tab1,tab2,tab3,tab4]
},
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['chromeTabs', 'column-2']
}
};


Unfortunately this has not worked for me. I would appreciate any directions.










share|improve this question
















I do have an initial datasource:



const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};


Once application loads (this is a chrome extension I am building for myself), I am getting opened tabs from chrome and would like to add them into this datasource as a new column.



Here's my attempt to do it:



export default (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: [{"chromeTabs"}, ...state.columnOrder]
};

default:
return state;
}
};


I expect to create a data object like below by above function:



const state = {
columns: [
{
id: 'chromeTabs,
title: 'chromeTabs',
tabs:
[tab1,tab2,tab3,tab4]
},
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['chromeTabs', 'column-2']
}
};


Unfortunately this has not worked for me. I would appreciate any directions.







javascript ecmascript-6 redux spread-syntax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 17:44







ilteris

















asked Nov 21 '18 at 17:34









ilterisilteris

227418




227418








  • 1





    What are you expecting the resulting redux state to look like?

    – Alexander Nied
    Nov 21 '18 at 17:39






  • 1





    "Unfortunately this has not worked for me..." In what way? Always quote any error message, etc., you're getting, or what behavior you're seeing that doesn't match what you expect.

    – T.J. Crowder
    Nov 21 '18 at 17:39











  • Also, I don't believe {"chromeTabs"} is a valid syntax of any kind.

    – Alexander Nied
    Nov 21 '18 at 17:40














  • 1





    What are you expecting the resulting redux state to look like?

    – Alexander Nied
    Nov 21 '18 at 17:39






  • 1





    "Unfortunately this has not worked for me..." In what way? Always quote any error message, etc., you're getting, or what behavior you're seeing that doesn't match what you expect.

    – T.J. Crowder
    Nov 21 '18 at 17:39











  • Also, I don't believe {"chromeTabs"} is a valid syntax of any kind.

    – Alexander Nied
    Nov 21 '18 at 17:40








1




1





What are you expecting the resulting redux state to look like?

– Alexander Nied
Nov 21 '18 at 17:39





What are you expecting the resulting redux state to look like?

– Alexander Nied
Nov 21 '18 at 17:39




1




1





"Unfortunately this has not worked for me..." In what way? Always quote any error message, etc., you're getting, or what behavior you're seeing that doesn't match what you expect.

– T.J. Crowder
Nov 21 '18 at 17:39





"Unfortunately this has not worked for me..." In what way? Always quote any error message, etc., you're getting, or what behavior you're seeing that doesn't match what you expect.

– T.J. Crowder
Nov 21 '18 at 17:39













Also, I don't believe {"chromeTabs"} is a valid syntax of any kind.

– Alexander Nied
Nov 21 '18 at 17:40





Also, I don't believe {"chromeTabs"} is a valid syntax of any kind.

– Alexander Nied
Nov 21 '18 at 17:40












1 Answer
1






active

oldest

votes


















2














{"chromeTabs"} is a syntax error. To add to the columnOrder array, just use "chromeTabs" without {} around it:



return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
// No {} -----^-----------^
};


Live Example:






const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};

const TABS_LOAD = "tabs-load";

const f = (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
};

default:
return state;
}
};

console.log(f(initState, {type: TABS_LOAD}));

.as-console-wrapper {
max-height: 100% !important;
}








share|improve this answer
























  • Thanks, let me do a little bit more digging, based on your code, mine should work too, however my console.log shows me the exact same result.

    – ilteris
    Nov 21 '18 at 17:52






  • 1





    @ilteris - Again, {"chromeTabs"} where a value is expected is a syntax error, so the code containing it will never run (because it can't be parsed at all).

    – T.J. Crowder
    Nov 21 '18 at 17:53











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%2f53417696%2fusing-spread-to-add-an-object-that-i-get-from-api-into-my-initial-datasource%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









2














{"chromeTabs"} is a syntax error. To add to the columnOrder array, just use "chromeTabs" without {} around it:



return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
// No {} -----^-----------^
};


Live Example:






const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};

const TABS_LOAD = "tabs-load";

const f = (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
};

default:
return state;
}
};

console.log(f(initState, {type: TABS_LOAD}));

.as-console-wrapper {
max-height: 100% !important;
}








share|improve this answer
























  • Thanks, let me do a little bit more digging, based on your code, mine should work too, however my console.log shows me the exact same result.

    – ilteris
    Nov 21 '18 at 17:52






  • 1





    @ilteris - Again, {"chromeTabs"} where a value is expected is a syntax error, so the code containing it will never run (because it can't be parsed at all).

    – T.J. Crowder
    Nov 21 '18 at 17:53
















2














{"chromeTabs"} is a syntax error. To add to the columnOrder array, just use "chromeTabs" without {} around it:



return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
// No {} -----^-----------^
};


Live Example:






const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};

const TABS_LOAD = "tabs-load";

const f = (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
};

default:
return state;
}
};

console.log(f(initState, {type: TABS_LOAD}));

.as-console-wrapper {
max-height: 100% !important;
}








share|improve this answer
























  • Thanks, let me do a little bit more digging, based on your code, mine should work too, however my console.log shows me the exact same result.

    – ilteris
    Nov 21 '18 at 17:52






  • 1





    @ilteris - Again, {"chromeTabs"} where a value is expected is a syntax error, so the code containing it will never run (because it can't be parsed at all).

    – T.J. Crowder
    Nov 21 '18 at 17:53














2












2








2







{"chromeTabs"} is a syntax error. To add to the columnOrder array, just use "chromeTabs" without {} around it:



return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
// No {} -----^-----------^
};


Live Example:






const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};

const TABS_LOAD = "tabs-load";

const f = (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
};

default:
return state;
}
};

console.log(f(initState, {type: TABS_LOAD}));

.as-console-wrapper {
max-height: 100% !important;
}








share|improve this answer













{"chromeTabs"} is a syntax error. To add to the columnOrder array, just use "chromeTabs" without {} around it:



return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
// No {} -----^-----------^
};


Live Example:






const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};

const TABS_LOAD = "tabs-load";

const f = (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
};

default:
return state;
}
};

console.log(f(initState, {type: TABS_LOAD}));

.as-console-wrapper {
max-height: 100% !important;
}








const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};

const TABS_LOAD = "tabs-load";

const f = (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
};

default:
return state;
}
};

console.log(f(initState, {type: TABS_LOAD}));

.as-console-wrapper {
max-height: 100% !important;
}





const initState = {
columns: [
{
id: 'column-2',
title: 'column-2',
tabs:
}
],
columnOrder: ['column-2']
};

const TABS_LOAD = "tabs-load";

const f = (state = initState, action) => {
switch (action.type) {
case TABS_LOAD:
return {
columns: [
{ id: 'chromeTabs', title: 'chromeTabs', tabs: action.payload },
...state.columns
],
columnOrder: ["chromeTabs", ...state.columnOrder]
};

default:
return state;
}
};

console.log(f(initState, {type: TABS_LOAD}));

.as-console-wrapper {
max-height: 100% !important;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 17:40









T.J. CrowderT.J. Crowder

687k12112221315




687k12112221315













  • Thanks, let me do a little bit more digging, based on your code, mine should work too, however my console.log shows me the exact same result.

    – ilteris
    Nov 21 '18 at 17:52






  • 1





    @ilteris - Again, {"chromeTabs"} where a value is expected is a syntax error, so the code containing it will never run (because it can't be parsed at all).

    – T.J. Crowder
    Nov 21 '18 at 17:53



















  • Thanks, let me do a little bit more digging, based on your code, mine should work too, however my console.log shows me the exact same result.

    – ilteris
    Nov 21 '18 at 17:52






  • 1





    @ilteris - Again, {"chromeTabs"} where a value is expected is a syntax error, so the code containing it will never run (because it can't be parsed at all).

    – T.J. Crowder
    Nov 21 '18 at 17:53

















Thanks, let me do a little bit more digging, based on your code, mine should work too, however my console.log shows me the exact same result.

– ilteris
Nov 21 '18 at 17:52





Thanks, let me do a little bit more digging, based on your code, mine should work too, however my console.log shows me the exact same result.

– ilteris
Nov 21 '18 at 17:52




1




1





@ilteris - Again, {"chromeTabs"} where a value is expected is a syntax error, so the code containing it will never run (because it can't be parsed at all).

– T.J. Crowder
Nov 21 '18 at 17:53





@ilteris - Again, {"chromeTabs"} where a value is expected is a syntax error, so the code containing it will never run (because it can't be parsed at all).

– T.J. Crowder
Nov 21 '18 at 17:53




















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%2f53417696%2fusing-spread-to-add-an-object-that-i-get-from-api-into-my-initial-datasource%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith