如果< notFound />路径执行(即:如果找不到页面,我想将类添加到页脚组件。
下面是我的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;组件得到渲染
答案 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} />