0

I'm using create-react-app . --template redux and I want to use spread syntax inside the configureStore reducer.

Like this:

import { configureStore } from '@reduxjs/toolkit';

import aReducer from './a/slice';
import bReducer from './b/slice';
import cReducer from './c/slice';

export default configureStore({
  reducer: {
    a: aReducer,
    b: bReducer,
    ...cReducer
  },
});

Inside the cReducer which is in its own folder with slice.js and utils.js, I have a few properties which I don’t want to have in the separate object inside the store, but I want it to be at the root of the store.

How can I achieve that?

zrna
  • 565
  • 1
  • 8
  • 20
  • There is no "[spread operator](https://stackoverflow.com/questions/44934828/is-it-spread-syntax-or-the-spread-operator)". There is [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax). – RobG Sep 08 '20 at 06:29
  • What's wrong with what you have ? Can you post your `c/slice` file – Ali Faris Sep 10 '20 at 07:20

2 Answers2

2

The intend of your question is not clear. Are you having problem with the spread uses or how to compose the store object.

below are a few solutions based of assumptions:

  1. If you want the properties prop1 and prop2 in the configureStore directly. just reference them and add them at the root level of store.

    import aReducer from './a/slice';
    import bReducer from './b/slice';
    import cReducer from './c/slice';
    
    const {prop1, prop2} = cReducer;
    
    export default configureStore({
      reducer: {
        a: aReducer,
        b: bReducer,           
      },   
        prop1,
        prop2,       
    }); 
    
  2. If by root you meant in the reducer object.

    import aReducer from './a/slice';
    import bReducer from './b/slice';
    import cReducer from './c/slice';
    
    const {prop1, prop2} = cReducer;
    
    export default configureStore({
      reducer: {
        a: aReducer,
        b: bReducer,
        prop1,
        prop2,
      },          
    });
    

you can also alias the props:

    const {prop1, prop2} = cReducer;
    
    export default configureStore({
      reducer: {
        a: aReducer,
        b: bReducer,            
      },     
       c: prop1,
       d: prop2,     
    });

Note: BTW if you want the cReducer also in the object you can simply add that as well, as you already have.

If you wish to add a subset of props of cReducer, you can compose a new object using lodash omit function. see omit

const cReducerWithout = omit(cReducer, 'propx')

and use the cReducerWithout in your store.

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
0

From ES2018 (ES9) You can do this:

import cReducer from './c/slice';

const { dontWantThis, dontWantThis2, ...restOfcReducer } = cReducer;

export default configureStore({
  reducer: {
    a: aReducer,
    b: bReducer,
    ...restOfcReducer
  },
});

Check this article for more: https://dmitripavlutin.com/javascript-object-destructuring/#8-rest-object-after-destructuring

Yonatan
  • 1,319
  • 4
  • 13
  • 32
  • That doesn't work. I do not get `restOfcReducer ` object in the store at all. – zrna Sep 11 '20 at 05:38
  • If you used the latest create-react-app, it should come with this feature available. https://create-react-app.dev/docs/supported-browsers-features/#supported-language-features Try create your project with `npx create-react-app . --template redux` to make sure you use the latest create-react-app version – Yonatan Sep 13 '20 at 08:04
  • @Zrna and you shouldn't, you should get properties of that object on your store. – zhuber Sep 15 '20 at 06:13