简体   繁体   English

如何在material-ui的导航栏中设置onClick?

[英]How do I set the onClick in the navbar of material-ui?

Below is the code for creating the navigation bar/header for an application.下面是为应用程序创建导航栏/标题的代码。 I have used material-ui.我用过 material-ui。

import React, { Component } from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/styles";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Typography from "@material-ui/core/Typography";
import IconButton from "@material-ui/core/IconButton";
import MenuIcon from "@material-ui/icons/Menu";
import AccountCircle from "@material-ui/icons/AccountCircle";
import ShoppingCartOutlinedIcon from "@material-ui/icons/ShoppingCartOutlined";

const styles = (theme) => ({
  root: {
    width: "100%",
  },
  flex: {
    flex: 1,
  },
  menuButton: {
    marginLeft: -12,
    marginRight: 20,
  },
});

class Nav extends Component {
  render() {
    const { classes } = this.props;

    return (
      <AppBar position="static" elevation={0}>
        <Toolbar>
          <IconButton
            className={classes.menuButton}
            color="contrast"
            onClick={this.props.toggleDrawer}
          >
            <MenuIcon />
          </IconButton>
          <Typography className={classes.flex} type="title" color="inherit">
            Pizza Shop
          </Typography>
          <div>
            <IconButton color="contrast" onClick={this.props.cart}>
              <ShoppingCartOutlinedIcon />
            </IconButton>
            <IconButton color="contrast" onClick={this.props.login}>
              <AccountCircle />
            </IconButton>
          </div>
        </Toolbar>
      </AppBar>
    );
  }
}

Nav.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(Nav);

I'm new to 'props' and I'm not sure what this.props.cart or this.props.login will do.我是“道具”的新手,我不确定this.props.cartthis.props.login会做什么。 I want to create a functionality where when I click on the above two icons, I'm directed to some another component, but I'm not sure how to do it.我想创建一个功能,当我单击上面的两个图标时,我会被定向到另一个组件,但我不知道该怎么做。 Please help me to understand it.请帮助我理解它。

Props are just parameters that the parent component sent to the children component. Props只是父组件发送给子组件的参数。 In your example this.props.cart and this.props.login are functions ( I am not sure about the cart , but it is used as a function ).在您的示例this.props.cartthis.props.login是函数(我不确定cart ,但它被用作 function )。 From your example, when you click on the icons, you will call cart and login functions sent from the parent.从您的示例中,当您单击图标时,您将调用父级发送的cartlogin功能。

The answer to your question "How do you set onClick" is you already doing it on the login function/method.您的问题“您如何设置 onClick”的答案是您已经在login功能/方法上进行了操作。 So you need to look at the parent component implementation.所以需要看父组件的实现。

Below I wrote a much more readable example, so feel free to look下面我写了一个更具可读性的示例,请随意查看

import React from 'react'

class ChildrenComponent extends React.Component {
  render() {
    // This is doing the same thing, just we call the function / method in little different way
    // return <div onClick={() => { this.props.onShoppingSubmit() }}>Aaa</div>
    return <div onClick={this.props.onShoppingSubmit}>Aaa</div>
  }
}

class ParentComponent extends React.Component {
  handleShoppingSubmit = () => {
    // Do what every that function needs to do
    console.log('Hi from parent')
  }

  render() {
    return (
      <div>
        <ChildrenComponent onShoppingSubmit={this.handleShoppingSubmit} />
      </div>
    )
  }
}

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

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