简体   繁体   English

在 React 中实现事件处理程序

[英]Implementing Event Handler in React

I only recently started coding in React and I'm in need of some assistance.我最近才开始在 React 中编码,我需要一些帮助。 I'm building a simple calculator and I'm trying to pass an event handler to a child component.我正在构建一个简单的计算器,并尝试将事件处理程序传递给子组件。 In the flow of the app, the App component is the parent component.在应用的流程中,App 组件是父组件。 It houses a child "Button Panel" component, which in turn houses child "Button" components as follows:它包含一个子“按钮面板”组件,该组件又包含子“按钮”组件,如下所示:

import React from 'react';
import Display from './display';
import ButtonPanel from './button-panel';
import method from '../logic/calculate';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      total: null,
      next: null,
      operation: null,
    };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick(buttonName) {
    // Updates this.state based on buttonName
  }

  render() {
    const { total } = this.state;
    return (
      <div id="app">
        <Display result={total} />
        <ButtonPanel onClick={this.handleClick} />
      </div>
    );
  }
}

export default App;

------------------------------------------------------------------------------------------------

import React from 'react';
import PropTypes from 'prop-types';
import Button from './button';

class ButtonPanel extends React.Component {
  constructor(props) {
    super(props);
    this.passInfo = this.passInfo.bind(this);
  }

  passInfo(buttonName) {
    const { handleClick } = this.props;
    handleClick(buttonName);
  }

  render() {
    return (
      <div>
        <div className="panel-row">
          <Button name="AC" color="white" onClick={this.passInfo} />
          <Button name="+/-" color="white" onClick={this.passInfo} />
          <Button name="%" color="white" onClick={this.passInfo} />
          <Button name="/" onClick={this.passInfo} />
        </div>
        <div className="panel-row">
          <Button name="7" color="white" onClick={this.passInfo} />
          <Button name="8" color="white" onClick={this.passInfo} />
          <Button name="9" color="white" onClick={this.passInfo} />
          <Button name="X" />
        </div>
        <div className="panel-row">
          <Button name="4" color="white" onClick={this.passInfo} />
          <Button name="5" color="white" onClick={this.passInfo} />
          <Button name="6" color="white" onClick={this.passInfo} />
          <Button name="-" onClick={this.passInfo} />
        </div>
        <div className="panel-row">
          <Button name="1" color="white" onClick={this.passInfo} />
          <Button name="2" color="white" onClick={this.passInfo} />
          <Button name="3" color="white" onClick={this.passInfo} />
          <Button name="+" />
        </div>
        <div className="panel-row">
          <Button name="0" color="white" wide onClick={this.passInfo} />
          <Button name="." color="white" onClick={this.passInfo} />
          <Button name="=" onClick={this.passInfo} />
        </div>
      </div>
    );
  }
}

ButtonPanel.propTypes = {
  handleClick: PropTypes.func.isRequired,
};

export default ButtonPanel;

------------------------------------------------------------------------------------------------

import React from 'react';
import PropTypes from 'prop-types';

class Button extends React.Component {
  constructor(props) {
    super(props);
    this.namePass = this.namePass.bind(this);
  }

  namePass(e) {
    const { name } = e.target;
    const { handleClick } = this.props;
    handleClick(name);
  }

  render() {
    const { name, color, wide } = this.props;
    const classes = color === 'white' && wide ? 'white wide' : color;
    return (
      <div
        className={classes}
        onClick={this.namePass}
        onKeyPress={this.namePass}
        role="button"
        tabIndex="-1"
      >
        {name}
      </div>
    );
  }
}

Button.propTypes = {
  name: PropTypes.string.isRequired,
  color: PropTypes.string,
  wide: PropTypes.bool,
  handleClick: PropTypes.func.isRequired,
};
Button.defaultProps = {
  color: 'orange',
  wide: false,
};

export default Button;


(please excuse the hard-coding, I'm trying to get everything working before refactoring). (请原谅硬编码,我试图在重构之前让一切正常工作)。

At the moment though, I'm getting the following errors:但目前,我收到以下错误:

1) "Warning: Failed prop type: The prop handleClick is marked as required in ButtonPanel , but its value is undefined in ButtonPanel" 1)“警告:失败的道具类型:道具handleClickButtonPanel标记为必需,但其值在 ButtonPanel 中undefined

2) "Warning: Failed prop type: The prop handleClick is marked as required in Button , but its value is undefined in Button" 2)“警告:失败的道具类型:道具handleClickButton标记为必需,但其值在 Button 中undefined

Any assistance in solving this would be greatly appreciated.任何解决此问题的帮助将不胜感激。

Change handleClick in proptypes to onClick OR rename onClick to handleClick.将 proptypes 中的 handleClick 更改为 onClick 或将 onClick 重命名为 handleClick。

The warning is coming from Proptypes.警告来自 Proptypes。 It is expecting handleClick prop for both custom components but you are using onClick instead.两个自定义组件都需要 handleClick 道具,但您使用的是 onClick 。

I would go with the latter as onClick is normally a default prop for many elements.我会选择后者,因为 onClick 通常是许多元素的默认道具。

ButtonPanel.propTypes = {
  onClick: PropTypes.func.isRequired,
};

Button.propTypes = {
  onClick: PropTypes.func.isRequired,
  ...
};

OR或者

<ButtonPanel handleClick={this.handleClick} />
<Button handleClick={this.handleClick} ..... />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM