3

I am pretty much new to redux and i am using redux-toolkit here.

Is there any way to export multiple slices from a single slice file in redux-toolkit??

example

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

const isAuthenticated = createSlice({
    name: isAuthenticated,
    initialState: false,
    reducers: {
        loginSuccess(state, action) {
            return true;
        },
        logout(state, action) {
            return false;
        },
    },
});

const currentUser = createSlice({
    name: currentUser,
    initialState: {},
    reducers: {
        setUserDetailsApi(state, action) {
            return action.user;
        },
    },
});

export const { loginSuccess, logout } = isAuthenticated.actions;
export const { setUserDetailsApi } = currentUser.actions;

export default isAuthenticated.reducer;

//how should i export currentUser.reducer??

Actually i am trying to replicate my code of vanilla redux here which has multiple reducer in a single file.

Please correct me if i am doing anything wrong here. Thanks

shiva shukla
  • 119
  • 2
  • 11
  • don't use export default if you are going to use export const. change everything to export const and import using `{ }` destructuring – Sinan Yaman Dec 25 '20 at 14:28
  • @SinanYaman i tried this export const{ isAuthenticated.reducer, currentUser.reducer }; but its a syntax error – shiva shukla Dec 25 '20 at 14:35

2 Answers2

4

To return multiple reducers in the same file you could use combineReducers

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

export default combineReducers({
    isAuthenticated: isAuthenticated.reducer,
    currentUser: currentUser.reducer
});

langen
  • 740
  • 5
  • 17
0
export {
  someName: isAuthenticated.reducer, 
  someOtherName: currentUser.reducer
}

and inside other component

import { someName, someOtherName } from '...'
Sinan Yaman
  • 5,714
  • 2
  • 15
  • 35
  • `SyntaxError:Unexpected token, expected "," (128:12)` `export { > 128 | someName: isAuthenticated.reducer, | ^ 129 | someOtherName: currentUser.reducer 130 | }` – shiva shukla Dec 25 '20 at 14:48
  • 1
    https://stackoverflow.com/questions/34645731/export-more-than-one-variable-in-es6 – Sinan Yaman Dec 25 '20 at 14:51