简体   繁体   中英

Uncaught TypeError: this.props.*** is not a function in React-redux app while calling a function from redux container

I have a react app with redux implemented and I'm getting this error

Uncaught TypeError: this.props.updateDimensions is not a function

while executing the App.js:-

class App extends Component {
  componentDidMount() {
    this.updateWindowDimensions();
    window.addEventListener("resize", this.updateWindowDimensions);
  }

  componentWillUnmount() {
    window.removeEventListener("resize", this.updateWindowDimensions);
  }

  updateWindowDimensions() {
    this.props.updateDimensions(window.innerWidth, window.innerHeight);
  }

  render() { .....

The AppContainer.js file:-

import { connect } from "react-redux";
import App from "./App";
import { UPDATE_DIMENSIONS } from "../actions";

const mapStateToProps = (state, ownProps) => {
  return {
    screenWidth: state.screenSize.screenWidth,
    screenHeight: state.screenSize.screenHeight
  };
};

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    updateDimensions: (screenWidth, screenHeight) => {
      dispatch({ type: UPDATE_DIMENSIONS, screenWidth, screenHeight });
    }
  };
};

const AppContainer = connect(mapStateToProps, mapDispatchToProps)(App);

export default AppContainer;

This is the index.js, where the AppContainer is being called:-

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore } from "redux";
import rootReducer from "./reducers";
import { BrowserRouter as Router, Route } from "react-router-dom";
import "./index.css";
import AppContainer from "./App";
import registerServiceWorker from "./registerServiceWorker";
import "semantic-ui-css/semantic.min.css";

let store = createStore(rootReducer);

ReactDOM.render(
  <Provider store={store}>
    <Router>
      <Route component={AppContainer} />
    </Router>
  </Provider>,
  document.getElementById("root")
);
registerServiceWorker();

I believe this is a common error and I have looked everywhere and there seems to be no obvious flaw in the code. Not sure where the issue is.

Figured it out.

in index.js:-

import AppContainer from "./App";

should be

import AppContainer from "./AppContainer";

ugh! This would have been so much easier if React threw an error saying 'AppContainer not found in ./App.js'

Update:-

I also had another error that was fixed by adding the following:-

  constructor(props) {
    super(props);
    this.updateWindowDimensions = this.updateWindowDimensions.bind(this);
  }

I think you forgot :

constructor(props) {
   super(props);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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