gpt4 book ai didi

javascript - 为什么我会得到 'Cannot read property ' props' of undefined'?

转载 作者:行者123 更新时间:2023-11-28 12:12:51 25 4
gpt4 key购买 nike

在我的 create-react-app/redux 中,我有一个“连接的组件”:

import React from "react";
import "./App.css";
import { connect } from "react-redux";

function App() {
debugger;
const { datas } = this.props;

debugger;
return <div className="App">{datas}}</div>;
}

const mapStateToProps = state => {
return {
datas: state
};
};

// const mapDispatchToProps = dispatch => {
// return {
// onRequestDog: () => dispatch({ type: "API_CALL_REQUEST_POST" })
// };
// };

export default connect(
mapStateToProps,
{}
)(App);

redux 在index.js 中配置:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import createSagaMiddleware from "redux-saga";
import { createStore, applyMiddleware } from "redux";
import { watcherSaga } from "./sagas";
import { Provider } from "react-redux";
import reducer from './redux.js'

//hookup saga
// create the saga middleware
const sagaMiddleware = createSagaMiddleware();

// dev tools middleware
const reduxDevTools =
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__();

// create a redux store with our reducer above and middleware
let store = createStore(reducer, reduxDevTools );

// run the saga
//sagaMiddleware.run(watcherSaga);


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

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.unregister();

当我运行此应用程序时,我在控制台中看到以下内容:

Cannot read property 'props' of undefined

如何调试此问题/原因是什么?

最佳答案

this 用于基于类的组件:

function App(props) {
debugger;
const { datas } = props;

debugger;
return <div className="App">{datas}</div>;
}

关于javascript - 为什么我会得到 'Cannot read property ' props' of undefined'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57097114/

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