简体   繁体   English

React Redux -> TypeError: Object(...) is not a function

[英]React Redux -> TypeError: Object(...) is not a function

So I'm learning react and I've split my code into seperate files which are, as follows ->ReduxDemo.js/reducers.js/store.js/actions.js Here is their content:所以我正在学习 react 并将我的代码拆分为单独的文件,如下所示 ->ReduxDemo.js/reducers.js/store.js/actions.js 以下是它们的内容:

ReduxDemo.js: ReduxDemo.js:

import React from 'react'
import {useSelector} from 'react-redux'
function ReduxDemo(){
    const cakeAmount = useSelector(state=>state.cakeCount)      
     return(
        <div>
            <div>
                <h1>Amount of cakes - {cakeAmount}</h1>
            </div>
        </div>)
    }
export default ReduxDemo;

reducers.js:减速器.js:

import {combineReducers} from 'redux'

const cakeState=
{
    cakeCount: 10,
}
const iceCreamState=
{
    iceCreamCount: 20
}

const cakeReducer=(state={cakeState},action)=>
        {
            
            switch(action.type)
            {
                case 'buyCake':
                    return{
                        ...state, 
                        cakeCount: state.cakeCount -1
                    }
                default:
                    return state
            }
            
        }
const iceCreamReducer=(state=iceCreamState, action)=>
        {
            switch(action.type)
            {
                case 'buyIceCream':
                    return{
                        ...state, 
                        iceCreamCount: state.iceCreamCount-1
                    }
                default: 
                    return state;
            }
        }

const reducers = combineReducers(
            {
                cake: cakeReducer,
                iceCream: iceCreamReducer
            })

export default reducers;

store.js:商店.js:

import reducers from './reducers'
import {createStore} from 'react'

const store = createStore(reducers)
store.subscribe(()=>
{
    console.log('store changed:', store.getState())
})
export default store;

actions.js:动作.js:

import store from './store'
export const buyCake=()=>
        {
            store.dispatch({type: 'buyCake'})
        }

    

App.js:应用程序.js:

import React from 'react';
import logo from './logo.svg';
import './App.css';
import ReduxDemo from './ReduxDemo'
import {Provider} from 'react-redux'
import store from './store'
function App() {

  return (
    <div>
        <Provider store={store}>
          <ReduxDemo></ReduxDemo>
        </Provider>
    </div>
  );
}

export default App;

for some obscure to me reason I get "TypeError: Object(...) is not a function", that is supposedly located at this line in store.js -> const store = createStore(reducers) Why is that and how can I fix it?出于对我来说一些晦涩的原因,我得到“TypeError: Object(...) is not a function”,据说它位于 store.js -> const store = createStore(reducers)中的这一行,为什么会这样,我该怎么做修理它?

createStore is not a function offered by React. createStore不是 React 提供的函数。 You need to import it from Redux:你需要从 Redux 导入它:

// store.js
import { createStore } from 'redux'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM