简体   繁体   English

在 React Child 中永远不会触发 onClick 事件

[英]onClick event never fires in React Child

I have a React-Redux application and I'm having issues launching a function on component click.我有一个 React-Redux 应用程序,但在单击组件时启动功能时遇到问题。 When the logout button is clicked, it should trigger onClick={this.handleLogout} .当注销按钮被点击时,它应该触发onClick={this.handleLogout} However, Redux dev tools and adding a console log in the handleLogout prove that the function never executes.但是,Redux 开发工具和在handleLogout添加控制台日志证明该函数永远不会执行。

Here are the two components, a standard navigation bar and a custom button component.这是两个组件,一个标准导航栏和一个自定义按钮组件。 I've left out some imports to shorten the code.我省略了一些导入来缩短代码。

Navigation Bar导航栏

class Navigation extends React.Component {
  handleLogout = () => {
    this.props.logOut();
    this.props.history.push("/login");
  }

  render() {
    return (
      <nav>
        <span className="nav-left">
          <Link className="light" to="/">
            Home
          </Link>
        </span>
        <span className="nav-right">
          {this.props.authenticated ? (
            <>
              <Button to="/account">Account</Button>
              <Button onClick={this.handleLogout} buttonStyle="danger">
                Log Out
              </Button>
            </>
          ) : (
            <Button to="/login">Login</Button>
          )}
        </span>
      </nav>
    );
  }
}

Navigation.propTypes = {
  authenticated: PropTypes.bool.isRequired,
  logOut: PropTypes.func.isRequired
};

export default withRouter(Navigation);

Button按钮

export default class Button extends Component {
  state = {
    classList: classNames({
      button: true,
      button_accent: this.props.buttonStyle === "accent",
      button_danger: this.props.buttonStyle === "danger"
    })
  };

  render() {
    return (
      <div className="button_container">
        <div
          className={classNames({
            button_wrapper: true,
            center: !this.props.left && !this.props.right,
            left: this.props.left,
            right: this.props.right
          })}
        >
          {
            this.props.to ? (
              this.props.regular ? (
                <a href={this.props.to} className={this.state.classList}>
                  {this.props.children}
                </a>
              ) : (
                <Link to={this.props.to} className={this.state.classList}>
                  {this.props.children}
                </Link>
              )
            ) : (
              <span className={this.state.classList}>
                {this.props.children}
              </span>              
            )
          }
        </div>
      </div>
    );
  }
}

Component Dev Tools component dev tools组件开发工具组件开发工具

You forgot to pass onClick to the span that represents a button without a link您忘记将 onClick 传递给表示没有链接的按钮的跨度

 export default class Button extends Component { state = { classList: classNames({ button: true, button_accent: this.props.buttonStyle === "accent", button_danger: this.props.buttonStyle === "danger" }) }; render() { return ( <div className="button_container"> <div className={classNames({ button_wrapper: true, center: !this.props.left && !this.props.right, left: this.props.left, right: this.props.right })} > { this.props.to ? ( this.props.regular ? ( <a href={this.props.to} className={this.state.classList}> {this.props.children} </a> ) : ( <Link to={this.props.to} className={this.state.classList}> {this.props.children} </Link> ) ) : ( <span onClick={this.props.onClick} className={this.state.classList}> {this.props.children} </span> ) } </div> </div> ); } }

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

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