简体   繁体   English

如何使用Material-UI AppBar onTitleClick属性在React中渲染新组件?

[英]How do I Render a new component in React using Material-UI AppBar onTitleClick Property?

I'm trying to render a new component using the onTiTleClick property. 我正在尝试使用onTiTleClick属性呈现一个新组件。 Here's my component's code 这是我组件的代码

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';

import AppBar from 'material-ui/AppBar';
import LoginButton from './LoginButton';
import LogoutButton from './LogoutButton';

class Header extends Component {
  constructor(props) {
    super(props);
    this.handleTitleClick = this.handleTitleClick.bind(this);
  }

  renderButton() {
    switch (this.props.auth) {
      case null:
        return
      case false:
        return <LoginButton />
      default:
        return <LogoutButton />
    }
  }

  handleTitleClick() {
    console.log(this)
    return(
      <Link to={this.props.auth ? '/courses' : '/'}></Link>
    );
  }

  render() {
    const styles = {
      title: {
        cursor: 'pointer',
      },
    };

    return(
      <AppBar
        title={<span style={styles.title}>QueueMe</span>}
        onTitleClick={this.handleTitleClick}
        iconElementRight={this.renderButton()}
        showMenuIconButton={false}
      />
    );
  }
}

/*
 * @input: redux state
 * Allows the component to access certain piece of the state as props
 * Reducers determine the key in the state
 */
function mapStateToProps(state) {
  return { auth: state.auth };
}

export default connect(mapStateToProps)(Header);

I'm passing a Link tag but I think it's not working because I'm only returning the link tag and not doing the actual redirecting. 我传递了Link标记,但是我认为它不起作用,因为我只返回了link标记,没有进行实际的重定向。 How can I defined a function that is able to redirect me to /courses or / and pass it to the onTitleClick property of the AppBar ? 如何定义一个函数,该函数能够将我重定向到/courses/并将其传递给AppBaronTitleClick属性? Thanks! 谢谢!

use the withRouter HOC - you'll get a history prop you can use to adjust the route programatically. 使用withRouter您将获得history道具,可用于以编程方式调整路线。

import { withRouter } from 'react-router'

class Component extends React.Component {
  render() {
    return (
      <button onClick={() => this.props.history.push('/courses')}>
        click me
      </button>
    )
  }
}

export default withRouter(Component)

I noticed you're also using react-redux . 我注意到您也在使用react-redux Something that really bothered me when working with a bunch of libraries that had their own HoCs was figuring out the eventual export. 当与一堆拥有自己的HoC的图书馆合作时,真正困扰我的事情是弄清楚最终的出口情况。 You'd end up with a bunch of intermediary components that just pollute your code 您最终会得到很多中间组件,这些组件只会污染您的代码

const x = connect(...)(Component)
const y = someOtherHoC(...)(x)
export default withRouter(x)

I highly recommend recompose - you can use the compose helper to chain them together 我强烈建议您recompose整理-您可以使用compose助手将它们链接在一起

export default compose(connect(...), someOtherHoC(...), withRouter)(Component)

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

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