shiva shukla
shiva shukla

Reputation: 185

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

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

Upvotes: 3

Views: 3346

Answers (2)

langen
langen

Reputation: 770

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
});

Upvotes: 4

Sinan Yaman
Sinan Yaman

Reputation: 5937

export {
  someName: isAuthenticated.reducer, 
  someOtherName: currentUser.reducer
}

and inside other component

import { someName, someOtherName } from '...'

Upvotes: 0

Related Questions