TypeError: Object(…) is not a function in react-redux

巧了我就是萌 提交于 2020-02-03 07:57:24

问题


While I was trying to createStore in react-redux I get this strange error. I don't know why as I did this before in different app there it was working fine.

TypeError: Object(...) is not a function
./src/index.js
C:/projects/facebook/facebook-react/src/index.js:14
  11 | import rootReducer from './rootReducer';
  12 | import registerServiceWorker from './registerServiceWorker';
  13 | 
> 14 | const store = createStore(
  15 |     rootReducer,
  16 |     composeWithDevtools(applyMiddleware(thunk))
  17 | );
View compiled
▶ 6 stack frames were collapsed.

this is my file

index.js

import { createStore, applyMiddleware} from 'redux';
import { composeWithDevtools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import App from './App';
import rootReducer from './rootReducer';
import registerServiceWorker from './registerServiceWorker';

const store = createStore(
    rootReducer,
    composeWithDevtools(applyMiddleware(thunk))
);


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

rootReducer

import { combineReducers } from 'redux'; import user from './reducers/user';

export default combineReducers({
    user });

reducer/user

import { USER_LOGGED_IN } from '../types';

export default function user(state = {}, action={}) {
    switch (action.type) {
        case USER_LOGGED_IN:
            return action.user;
        default:
            return state;
    } }

回答1:


I had the same issue: I could get it to work on a test app and couldnt move it across to my main project. Checked all of the code 100 times and researched widely but couldnt find anything to make connect() work without it throwing "TypeError: Object(…) is not a function"

In the end all that was required was to update react and react-DOM in my main project as i was running a new version of redux with old react.

Just run the below in your project folder.

npm update

Hope this helps you




回答2:


This error ocurred when:

  1. react-redux not available in runtime. Please check that it correctly bundled or available as global (if used from CDN)

  2. versions of react, redux incompatible with react-redux. Update some of them to the latest



来源:https://stackoverflow.com/questions/52036086/typeerror-object-is-not-a-function-in-react-redux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!