如果<notfound>组件被执行,则将类添加到<footer>组件。反应 - 终极版

时间:2016-11-10 10:39:17

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

如果&lt; notFound /&gt;路径执行(即:如果找不到页面,我想将类添加到页脚组件。

下面是我的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

我们可以在全局设置某些内容,还是可以获取路由名称,以便我们可以按照&lt; notFound /&gt;组件得到渲染

1 个答案:

答案 0 :(得分:1)

使用您传递给this.props.children然后在<NotFound />

中的回调
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