Search code examples
reactjsreact-reduxredux-toolkit

Redux-toolkit: Store does not have a valid reducer . Make sure the argument passed to combineReducers is an object whose values are reducers


i dont know why its showing me an error i not even started the coding yet i just writtened a configureStore syntax given by documentation

and i uploaded into index.js file via provider

here is my app .js

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <h1>Redux Tool Kit</h1>
    </div>
  );
}

export default App;

here is my store ,js

import { configureStore } from '@reduxjs/toolkit'
const store = configureStore({
  reducer: {},
})
export default store;

here is my index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import {Provider} from 'react-redux'
import  store  from './store/store'
import App from './App';
import reportWebVitals from './reportWebVitals';

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

// If you want to start measuring performance in your app, pass a function


and result is error store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.


Solution

  • You haven't defined any reducer function in your app

    import { configureStore,createSlice } from '@reduxjs/toolkit';
    
    const reducerSlice = createSlice({
      name: 'store',
      initialState: {},
      reducers: {
         someAction: function() {
    
         }
      }
    })
    
    const store = configureStore({
      reducer: {
        someReducer: reducerSlice.reducer,
      }
    })
    
    export default store;
    

    You can also create multiple slices/reducers in your app and use combineReducers and pass it to reducer object in configureStore

    NOTE: combineReducer is also available as an import from @reduxjs/toolkit