【问题标题】:If <notFound/> component get executed add class to <footer/>component . React-Redux如果 <notFound/> 组件被执行添加类到 <footer/> 组件。 React-Redux
【发布时间】:2017-03-24 08:36:15
【问题描述】:

如果 rout 被执行(即:如果未找到页面被渲染,我想将类添加到页脚组件。

下面是我的 index.jsx 渲染函数

    render() {
    return (
        <div>
            <Navbar />
            <div className="pageData">
                {this.props.children}
                // <notFound/> component will get rendered here
            </div>
            <Footer/>
            <LoginPopup />
        </div>
    )
}

下面是我的 routes.jsx

 import React from 'react'
import { Route, IndexRoute } from 'react-router'
import App from 'layout/app'
import Home from 'pages/home'
import MyWagers from 'containers/myWagersContainer'
import Wagers from 'containers/wagersContainer'
import NotFound from 'pages/notFound'

const ROUTES = (
<Route path='/' component={App}>
    <IndexRoute component={Home} />
    <Route path="/wagers(/:trackCode)(/:raceNum)" component={Wagers} >
        <Route path="*" component={() => (<NotFound status = "404" />)}/>
    </Route>
    <Route path="/mywagers" component={MyWagers} />
    <Route path="*" name = "notFound" component={() => (<NotFound status =  "404" />)}/>
</Route> ) 
export default ROUTES

那么我们可以全局设置一些东西,或者我们可以获取路由名称,以便我们可以根据 组件渲染在页脚组件中添加类

【问题讨论】:

    标签: reactjs react-redux react-router-redux


    【解决方案1】:

    使用您传递给 this.props.children 然后在&lt;NotFound /&gt; 中的回调

    componentWillMount() {
      this.props.setFooterClass('myclass');
    }
    
    componentWillUnmount() {
      this.props.setFooterClass('');
    }
    

    在 index.js 中:

     <NotFound setFooterClass={myClass => this.setState({ footerClass: myClass })} />
    

     <Footer className={this.state.footerClass} />
    

    Pass props to children

    【讨论】:

    • 我没有得到这个。如果未找到路由被执行,我们如何向页脚组件发送回调
    猜你喜欢
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2018-08-15
    相关资源
    最近更新 更多