如何解决React和Redux抛出无法读取未定义错误的属性'props'?

时间:2018-12-20 11:15:08

标签: javascript reactjs redux react-redux

我是Redux的新手,具有在React中的经验。我有两个组件ButtonAppBar和CriticalIssues。我试图通过单击ButtonAppBar中的按钮来显示CriticalIssues组件。单击按钮时ButtonAppBar引发无法读取未定义的属性'props'。我已经坚持了很长时间,我们将不胜感激。

我的代码是:

ButtonAppBar.js

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import styled from 'styled-components'
import CriticalIssues from '../CriticalIssues'

import { showCriticalBugs } from 'containers/App/actions';
import { connect } from 'react-redux';
import { show_critical } from '../../containers/App/sagas';

const styles = {
    root: {
        flexGrow: 1,
    },
    grow: {
        flexGrow: 1,
        textAlign: "left"
    },
    menuButton: {
        marginLeft: -12,
        marginRight: 20,
    },
};

const Button = styled.button`
background: linear-gradient(45deg, #fe6b8b 30%, #ff8e53 90%);
border-radius: 3px;
border: 0;
color: white;
height: 48px;
padding: 0 30px;
box-shadow: 0 3px 5px 2px rgba(255, 105, 135, .3);
`

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

    componentDidMount(){
        this.showCritical();
    }

    showCritical(){
        this.props.dispatch(showCriticalBugs());
    }

    render() {
        return (
            <div>
                <AppBar position="static">
                    <Toolbar>

                        <Typography variant="h6" color="inherit">
                            Regression 360
                         </Typography>
                        <Button onClick={this.showCritical} color="inherit">Predict Critical Bugs</Button>
                    </Toolbar>
                </AppBar>
            </div>
        );
    };
}
ButtonAppBar.propTypes = {
    dispatch: PropTypes.func,
  };

export default connect()(ButtonAppBar);

sagas.js

import {showCriticalBugs} from './actions'
import { call, cancel, select, take, takeLatest, put } from 'redux-saga/effects';

export function* show_critical (){
    yield put(showCriticalBugs());
}

actions.js

export const SHOW_CRITICAL = 'SHOW_CRITICAL';

export function showCriticalBugs() {
    return {
      type: SHOW_CRITICAL,
    };
  }

reducer.js

import SHOW_CRITICAL from './actions'

const initialState = fromJS({
    visible: false
  });
  

  function appReducer(state = initialState, action) {
    switch (action.type) {
      case SHOW_CRITICAL:
        console.log("m here");
        return state.set('visible', true);
    }
}
    

export default appReducer;

CriticalIssue.js

import React from 'react';
import PropTypes from 'prop-types'
import { connect } from 'react-redux';
import { Card, Typography, Button } from '@material-ui/core'


var mainDiv = {
  padding: 10,
  width: "auto",
  fontFamily: "monospace",
  textAlign: "center"
};


class CriticalIssues extends React.Component {
  componentDidMount(){
    console.log(this.props.visible)
  }
  render() {
    if (this.props.visible) {
      return (
        <Card>
          <div style={mainDiv}>
            <Typography variant={'h1'} color={"error"}>3</Typography>
            <Typography component={"h3"}>Critical Issues</Typography>
          </div>
        </Card>
      );
    }
    else return (null);
  }
}

function mapStateToProps(state) {
  return {
    visible: state.getIn(['global', 'visible']),
  };
}

export default connect(mapStateToProps)(CriticalIssues);

1 个答案:

答案 0 :(得分:0)

您必须绑定函数才能在方法中访问“ this”关键字 你可以用这个

<Button onClick={this.showCritical.bind(this)} color="inherit">Predict Critical Bugs</Button>

或者您也可以像下面那样在构造函数中绑定方法

constructor(props){
  super(props)
  this.showCritical = this.showCritical.bind(this)
}