Search code examples
reactjsdecoratorreduxreact-reduxecmascript-2016

react-redux's @connect produces the error 'connect.js:129Uncaught TypeError: Cannot read property 'store' of undefined'


When running my app (successfully bundled with webpack+babel with decorators support), the console shows:

'connect.js:129 Uncaught TypeError: Cannot read property 'store' of undefined'

Nothing else is showing on the screen All packages are up to date.

Any ideas? thanks.

app.js:

import React from 'react'
import ReactDOM from 'react-dom'
import Main from './components/main.jsx'

import { combineReducers } from 'redux'
import { Provider, connect } from 'react-redux'
import { createStore } from 'redux'

import * as mainActions from './actions/main_a'
import * as reducers from './reducers/main_r'

const reducer = combineReducers(reducers)
const store = createStore(reducer)

let mapDispatchToProps = (dispatch) => ({
    fireTestAction: () => dispatch(mainActions.testAction( true ))
});

let mapStateToProps = () => ({})

@connect(mapStateToProps, mapDispatchToProps)
export default class App extends React.Component{
    render() {
        return (
            <Provider store={store}>
                <Main txt='Fire test action' action={ fireTestAction} />
            </Provider>,
                document.getElementById('app')
        )
    }
}

var app = new App;
app.render();

package.json:

"devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.10.4",
    "babel-loader": "^6.2.4",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.5.0",
    "gulp": "^3.9.1",
    "gulp-exec": "^2.1.2",
    "gulp-nodemon": "^2.1.0",
    "gulp-shell": "^0.5.2",
    "gulp-webpack": "^1.5.0",
    "react-hot-loader": "^1.3.0",
    "redux-devtools": "^3.3.1",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },
  "dependencies": {
    "express": "^4.14.0",
    "react": "^15.1.0",
    "react-dom": "^15.1.0",
    "react-redux": "^4.4.5",
    "redux": "^3.5.2"
  }
}

Solution

  • It's because you are using the connect on the same component where you use the provider.

    <Provider store>

    Makes the Redux store available to the connect() calls in the component hierarchy below. Normally, you can’t use connect() without wrapping the root component in <Provider>.

    https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store

    You should use the connect on the <Main> component.

    EDIT:

    Also, if the App component is your entry point, you don't need to export it, and you don't need to do this:

    var app = new App; app.render(); .

    Instead, use ReactDOM to render the app. Like:

    ReactDOM.render(
     <Provider store={store}>
      <Main txt='Fire test action' action={ fireTestAction} />
     </Provider>,
     document.getElementById('app')
    )