未捕获的TypeError:无法读取未定义的属性'props'(react.js)(非常简单)

时间:2018-08-31 17:27:58

标签: javascript reactjs redux react-redux

我遵循了有关Redux的本入门教程:textvid

除“增量”按钮外,所有其他功能均有效。当我单击增量按钮时,出现此错误:

  

未捕获的TypeError:无法读取未定义的属性“ props”

看看会发生什么:gif

为什么我执行“ mapStateToProps”时会收到该错误?

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import Test_left from './eo_app.jsx';
import { Provider } from 'react-redux';
import { createStore } from "redux";

const initialState = {
    count: 21
};

const reducer = (state = initialState, action) => {
    console.log('reducer', action);

    switch (action.type) {
        case 'INCREMENT':
            return { count: 55 };
        default:
                return state;
    }
};

const store = createStore(reducer);


const App = () => (
  <Provider store={store}>
    <Test_left />
  </Provider>
);

ReactDOM.render(<App />, document.getElementById('target'));

//// store.dispatch({type: 'INCREMENT' }); this will work as expected

eo_app.jsx

import React from 'react';
import { connect } from 'react-redux';


class Test_left extends React.Component {
    constructor(props) {
        super(props);
    }

    increment () {
    this.props.dispatch({ type: 'INCREMENT' }); // <== ERROR IS RAISED HERE
  }

    render() {
        console.log('props:', this.props)
        return (
                <React.Fragment>
                    <h1> WE GOT RENDERED </h1>
                    <h1>{this.props.count}</h1>
                    <button onClick={this.increment}> Increment </button>
                </React.Fragment>
        )
    };
}

const mapStateToProps = state => ({
    count: state.count
})

export default connect(mapStateToProps)(Test_left);

2 个答案:

答案 0 :(得分:2)

编辑:

实际上有两个问题:

首先,您需要将increment函数绑定到类本身。这是一个常见的“陷阱”,您可以在这里阅读-https://reactjsnews.com/es6-gotchas

第二个是,使用react-reduxconnect函数,您需要将redux dispatch函数映射到prop。这是通过您在connect调用中传递的第二个功能来完成的:mapDispatchToProps

您的外观可能是:

const mapDispatchToProps = dispatch => ({
    handleClick: () => dispatch({ type: 'INCREMENT' })
});

有了这两个更改,您的组件将看起来像

class Test_left extends React.Component {
    constructor(props) {
        super(props);

        // Bind `increment` to this class's scope
        this.increment = this.increment.bind(this);
    }

    increment () {
        // Use newly created prop function to dispatch our action to redux
        this.props.handleClick();
    }

    // ...
}

const mapStateToProps = state => ({
    count: state.count
});
const mapDispatchToProps = dispatch => ({
    handleClick: () => dispatch({ type: 'INCREMENT' })
});

export default connect(mapStateToProps, mapDispatchToProps)(Test_left);

答案 1 :(得分:1)

执行以下操作时:this.props.dispatch,它将调用与连接映射的调度。但是您没有用于调度的地图:

export default connect(mapStateToProps)(Test_left);

因此,将前面的代码替换为:

export default connect(mapStateToProps, mapDispatchToState)(Test_left);

现在,为mapDispatchToState定义函数并在其中调用分派。


我忘记提及的关键问题是确保绑定this,因为在方法内部调用时this将是未定义的。或者,您可以分配一个public class method

increment = () => { // now, this will be accessible

PS:请参见this post,为什么需要使用地图调度而不是直接调度。请勿将连接映射与直接分配混合在一起。