javascript - 在使用 redux 的 react 组件中访问 this.props 时未定义

标签 javascript reactjs redux react-redux

我遇到了以下问题,我在 React 中创建了一个组件,我主要尝试使用 this.props.dispatch() 方法。但是,当我尝试在我创建的函数中引用 this.props 时,我在控制台中收到以下错误消息。

Uncaught TypeError: Cannot read property 'dispatch' of undefined

问题: 为什么不能在我自己添加的函数中使用 this.props 例如handleResize()this.props 可用于默认 react 函数,例如 componentWillMount()、componentDidMount()、

这是导致上述错误的函数。

handleResize() {
        console.log( "handle function getting called" );
        this.props.dispatch( changeListHeight( window.innerHeight ) );
    }

这是我的完整 react 组件

import React, {Component} from 'react';
import Avatar from 'material-ui/Avatar';
import List from 'material-ui/List/List';
import ListItem from 'material-ui/List/ListItem';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import { connect } from "react-redux";
import { fetchMessages } from '../actions/messageActions';
import { changeListHeight } from '../actions/appActions';


import {
    blue300,
    indigo900,
    orange200,
    deepOrange300,
    pink400,
    purple500,
} from 'material-ui/styles/colors';

@connect((store) => {
    return {
        messages: store.messages.messages,
        app: store.app
    };
})
class Items extends Component {

    constructor(props) {
        super(props);
        this.props.dispatch( changeListHeight( window.innerHeight ) );
    }

    componentWillMount() {
        this.props.dispatch( fetchMessages() );
    }

    componentDidMount() {
        console.log( this.props );
        window.addEventListener('resize', this.handleResize ); //event to handle resizing the message list
        this.refs.chatList.onscroll = function() { this.handleScroll }; //event to keep the freshest messages in view
    }

    handleResize() {
        console.log( "handle function getting called" );
        this.props.dispatch( changeListHeight( window.innerHeight ) );
    }

    handleScroll() {
        console.log( "handle function getting called" );
        //console.log(this.refs.chatList);
        //let isScrolledToBottom = this.chatList.scrollHeight - this.chatList.clientHeight <= this.chatList.scrollTop + 1;
    }

    render() {

        let listStyle = {
            height: (this.props.app.chatListHeight - (35 + 64 + 8 + 135)),
            overflowY: "auto"
        }

        let listItemStyle = {
            margin: 5,
        };

        return (
             <MuiThemeProvider>
                <List className="chat-list" style={listStyle} ref="chatList">
                    {this.props.messages.map(function(message){
                        return <ListItem
                            key={message.id}
                            disabled={true}
                            leftAvatar={
                                <Avatar
                                    color={deepOrange300}
                                    backgroundColor={purple500}
                                    size={30}
                                    style={listItemStyle}
                                >
                                {message.name.charAt(0)}
                                </Avatar>
                            }>
                            {message.msg}
                        </ListItem>;
                    })}
                </List>
             </MuiThemeProvider>
        );
    }
}

export default Items;

最佳答案

你在调用 this.handleResize 时使用了错误的上下文

替换:

window.addEventListener('resize', this.handleResize );

window.addEventListener('resize', this.handleResize.bind(this) );

或者在constructor中绑定(bind)这个函数

this.handleResize = this.handleResize.bind(this)

关于javascript - 在使用 redux 的 react 组件中访问 this.props 时未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38629292/

相关文章:

javascript - :active selector后触发点击事件

javascript - 尝试自定义 Facebook 分享按钮时出错

php - 没有内容类型的空 $_POST

javascript - 父组件和子组件之间的 2 种方式事件绑定(bind)不起作用

reactjs - 如何处理 typescript 中的数字输入?

reactjs - 为什么在handleLogin方法中this.props未定义?

angular - 在 Angular(typescript) 中使用 tassign 的 Redux 试图加入 state.array 和 action.array 但获取 state.array.join 不是一个函数?

javascript - CORS,防止带有授权 header 的请求预检

javascript - Apollo GraphQL,有没有办法在查询期间操纵正在写入缓存的数据?

reactjs - React-Redux 中状态的生命周期是多少?