【问题标题】:React with FlowRouter: How to show/hide component based on route与 FlowRouter 反应:如何根据路由显示/隐藏组件
【发布时间】:2016-12-29 17:42:18
【问题描述】:

我有一个使用React 开发的Meteor 应用程序,我使用FlowRouter 进行路由。我的项目主要AppContainer 有一堆组件,其中一个是页脚。

class AppContainer extends Component {
    render() {
        return(
            <div className="hold-transition skin-green sidebar-mini">
                <div className="wrapper">
                    <Header user={this.props.user} />
                    <MainSideBar />
                    {this.props.content}
                    <Footer />
                    <ControlSideBar />
                    <div className="control-sidebar-bg"></div>
                </div>
            </div>
        )
    }
}

我有几条通往各个聊天室的路线:

例如

/chatroom/1
/chatroom/2
/chatroom/3

如果路由是/chatroom/&lt;anything&gt;,我有办法隐藏&lt;Footer /&gt; 组件吗?

【问题讨论】:

    标签: reactjs flow-router


    【解决方案1】:

    您也许可以通过检查当前路径来进行条件渲染。

    如果/chatroom/后面的&lt;anything&gt;部分(我假设它是一个参数)不重要,并且如果你没有任何其他以聊天室开头的路由,你可以试试这个:

     const currentPath = window.location.pathname
    {!currentPath.includes('chatroom') ? <Footer /> : null }
    

    所以你的代码应该是这样的:

    class AppContainer extends Component {
        render() {
           currentPath = window.location.pathname
            return(
                <div className="hold-transition skin-green sidebar-mini">
                    <div className="wrapper">
                        <Header user={this.props.user} />
                        <MainSideBar />
                        {this.props.content}
                        {!currentPath.includes('chatroom')
                        ? <Footer /> 
                        : null }
                        <ControlSideBar />
                        <div className="control-sidebar-bg"></div>
                    </div>
                </div>
            )
        }
    }
    

    如果&lt;anything&gt;部分很重要和/或您有其他以聊天室开头的路由,您可以先获取路由的参数

    const param = FlowRouter.getParam('someParam');
    

    然后通过检查当前路径是否包含 chatroom/:param 来进行条件渲染,如下所示:

      const currentPath = window.location.pathname
    {!currentPath.includes(`chatroom/${param}`) ? <Footer /> : null }
    

    所以你的代码看起来像这样

     class AppContainer extends Component {
            render() {
               const currentPath = window.location.pathname
               const param = FlowRouter.getParam('someParam');
                return(
                    <div className="hold-transition skin-green sidebar-mini">
                        <div className="wrapper">
                            <Header user={this.props.user} />
                            <MainSideBar />
                            {this.props.content}    
                            {!currenPath.includes(`chatroom/${param}`)
                            ? <Footer /> 
                            : null }
                            <ControlSideBar />
                            <div className="control-sidebar-bg"></div>
                        </div>
                    </div>
                )
            }
        }
    

    【讨论】:

      【解决方案2】:

      您还可以使用以下语法。它完成的事情与 Cagri Yardimci 提供的第一个示例相同。

      { !currentPath.includes('chatroom') && <Footer /> }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-21
        • 1970-01-01
        • 2018-05-22
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多