1

I receive an error when following a YouTube crash course on redux:

redux.js:158 Uncaught Error: Expected the root reducer to be a function. Instead, received: ''

at createStore (redux.js:158:1) | at redux.js:682:1 | at createStore (redux.js:154:1) | at ./src/state/store.js (store.js:5:1)

I suspect the error is inside store.js, but I'm not able to debug it. And, why '' is returned? Very much grateful for your help.

store.js

import { legacy_createStore as createStore, applyMiddleware } from "redux";
import reducers from "./reducers/index"
import thunk from "redux-thunk"

export const store = createStore(
    reducers,
    {},
    applyMiddleware(thunk)
) 

reducers/index.js

import { combineReducers } from "redux";
import accountReducer from "./accountReducer";

const reducers = combineReducers ({
    account : accountReducer
});

export default reducers;

reducers/accountReducer.js

const reducer = (state = 0, action) => {
    switch (action.type) {
        case "deposit":
            return state + action.payload;
        case "withdraw":
            return state - action.payload;
        default:
            return state
    }
};

export default reducer;

App.js

import {useSelector, useDispatch} from "react-redux"
import {bindActionCreators} from "redux"
import {actionCreators} from "./state/index"

function App() {

  const account = useSelector((state) => state.acoount );
  const dispatch = useDispatch()

  const {depositMoney, withdrawMoney} = bindActionCreators(actionCreators, dispatch)
  

  return (
    <div className="App">
      <h1>{account}</h1>
      <button onClick={() => depositMoney(1000)}>Deposit</button>
      <button onClick={() => withdrawMoney(1000)}>Withdraw</button>
    </div>
  );
}

export default App;

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App' ;
import reportWebVitals from './reportWebVitals';
import { Provider } from 'react-redux';
import {store} from './state/store'
const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(
  <React.StrictMode>
    <Provider store = {store}>
    <App />
    </Provider>
  </React.StrictMode>
);

reportWebVitals();
ronzenith
  • 341
  • 3
  • 11
  • Note that using redux directly is deprecated, the redux team is pushing [redux toolkit](https://redux-toolkit.js.org/). Also if you don't know how to use redux (which is fine, everybody starts somewhere) maybe pick a more minimalistic example? If you're still learning don't write more than about 10 lines of code without running it. Maybe even if you're not still learning. – Jared Smith Aug 14 '22 at 11:08
  • What's versions of those packages you use? – raina77ow Aug 14 '22 at 11:20
  • @JaredSmith Thanks for your advice. This crash course is already the simplest and clearest I've ever come across. The problem occurs in the early stage. Would be very much appreciated if you can introduce a minimal learning project. – ronzenith Aug 14 '22 at 11:59
  • @raina77ow "react": "^18.2.0", "react-dom": "^18.2.0", "react-redux": "^8.0.2", "react-scripts": "5.0.1", "redux": "^4.2.0", "redux-thunk": "^2.4.1", "web-vitals": "^2.1.4" – ronzenith Aug 14 '22 at 12:00
  • Can you check what's value of `reducers` immediately before `createStore` step? It seems there might be a problem with importing this module (however the syntax is correct). – raina77ow Aug 14 '22 at 12:03
  • 1
    @ronzenith that crash course is teaching you a pre-2019 style of Redux that we really do not recommend writing any more. It might still have some merit to know what Redux does internally for learning purposes, but nowadays you would only write about 1/4 the amount of code, and it would look very differently. Please follow the [official Redux tutorial](https://redux.js.org/tutorials/essentials/part-1-overview-concepts) instead. – phry Aug 14 '22 at 12:10
  • @raina77ow unfortunately, unable to show any return with console.log(account) in App.js. Actually I can't console.log anything with this error... – ronzenith Aug 14 '22 at 12:16
  • Why do you need `console.log(account)` to check `reducers` value? This should be added to `store.js` file. – raina77ow Aug 14 '22 at 12:19
  • In fact, if your code is not minified, you can just put a breakpoint in your browser devtools. – raina77ow Aug 14 '22 at 12:21
  • @raina77ow supposed to be 0 in the beginning. – ronzenith Aug 14 '22 at 12:22

0 Answers0