【问题标题】:I want to disable/ did not want to show "header" on specific page of my React app我想禁用/不想在我的 React 应用程序的特定页面上显示“标题”
【发布时间】:2019-07-11 07:29:57
【问题描述】:

我正在尝试在我的 react 应用程序的特定页面上隐藏/禁用“标题”,下面给出的是我的组件的代码:

我想在“/dct”路径上隐藏标题

export default function (WrappedComponent,theme) {
  class Authentication extends PureComponent {
    constructor(props) {
      super(props);
      this.state = { };
    }


    componentDidUpdate() {
      this.checkAuth();
    }

    checkAuth() {
      const { isLoggedIn, history: { push} } = this.props;
      if (!isLoggedIn) {
        push('/')
      }
    }

    render() {
      return (
        <Fragment>
     <article data-theme={theme}>
          {this.checkAuth()}
          <Header />
          <main>
            {this.props.isLoggedIn && <WrappedComponent />}
          </main>
          <Footer />
     </article>
        </Fragment>
      );
    }
  }

 const mapStateToProps = (state) => {
   return {
     isLoggedIn: state.login.loggedIn
   }
 } 
  return connect(mapStateToProps)(Authentication);
}

提前致谢

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    你需要像这样检查你的路径名:window.location.pathname !== "/dct"

    例如,

    export default function (WrappedComponent,theme) {
      class Authentication extends PureComponent {
        constructor(props) {
          super(props);
          this.state = { };
        }
    
    
        componentDidUpdate() {
          this.checkAuth();
        }
    
        checkAuth() {
          const { isLoggedIn, history: { push} } = this.props;
          if (!isLoggedIn) {
            push('/')
          }
        }
    
        render() {
          return (
            <Fragment>
         <article data-theme={theme}>
              {this.checkAuth()}
              {window.location.pathname !== "/dct && ( <Header /> )}
              <main>
                {this.props.isLoggedIn && <WrappedComponent />}
              </main>
              <Footer />
         </article>
            </Fragment>
          );
        }
      }
    
     const mapStateToProps = (state) => {
       return {
         isLoggedIn: state.login.loggedIn
       }
     } 
      return connect(mapStateToProps)(Authentication);
    }
    

    让我知道这是否有效。

    【讨论】:

    • 它成功了,感谢你,现在我知道了这背后的逻辑
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多