1
import { configureStore } from "@reduxjs/toolkit";
import userReducer from "./userSlice";

export default configureStore({
  reducer: {
    user: userReducer,
  },
});
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { Provider } from "react-redux";
import store from "./redux/store";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Redirect,
} from "react-router-dom";

ReactDOM.render(
  <React.StrictMode>
    <Router>
      <Provider store={store}>
        <App />
      </Provider>
    </Router>
  </React.StrictMode>,
  document.getElementById("root")
);

The above is how I access the one reducer inside of configureStore.

What if:

export default configureStore({
  reducer: {
    user: userReducer,
    second_reducer: second_reducer,
  },
});

how do I access second_reducer?

  • What do you mean by "accessing a reducer"? A reducer is not something that can be accessed. Are you wondering about how to dispatch actions to the second reducer? Or how to select the slice of state handled by the second reducer? Did you try anything? Did you read the docs? – Guillaume Brunerie Mar 15 '22 at 20:21

1 Answers1

2

Something like this

import { useSelector } from 'react-redux'

const state = useSelector((store) => store.second_reducer.state)
Aaron Vasilev
  • 438
  • 2
  • 6
  • Your original answer was not even syntactically correct Javascript, but I see you've edited it now. There are still a few issues in it, though: you're using useSelector outside a component which is not allowed (I understand that it is not meant to be a complete file, but still, it's a common beginner mistake). Also the argument to the selector should be called `state` and not `store` as the store is something else in Redux terminology that should not be confused with the state. – Guillaume Brunerie Mar 15 '22 at 20:42
  • Oh, thanks for explaining me this. I'll try to be more exact! – Aaron Vasilev Mar 15 '22 at 20:48