gpt4 book ai didi

javascript - 从 redux 容器调用函数时,未捕获 TypeError : this. props.*** 不是 React-redux 应用程序中的函数

转载 作者:行者123 更新时间:2023-12-03 03:12:59 24 4
gpt4 key购买 nike

我有一个实现了 redux 的 React 应用程序,但收到此错误

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

执行 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() { .....

AppContainer.js 文件:-

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;

这是调用 AppContainer 的 index.js:-

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();

我相信这是一个常见的错误,我已经到处查看,代码中似乎没有明显的缺陷。不确定问题出在哪里。

最佳答案

想通了。

在index.js中:-

从“./App”导入AppContainer;

应该是

从“./AppContainer”导入AppContainer;

呃!如果 React 抛出一个错误说“在 ./App.js 中找不到 AppContainer”,这会容易得多

更新:-

我还遇到了另一个错误,已通过添加以下内容修复:-

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

关于javascript - 从 redux 容器调用函数时,未捕获 TypeError : this. props.*** 不是 React-redux 应用程序中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46887113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com