我正在从Link
中提到的路线发送React组件<Route path='/StartCompaign' render={(props) => <CampaignStart {...props} isDashboard={true} /> } />
我正在另一个组件中获得道具
{props.childern.render.isDashboard ? <Header /> : "NO" }
APP.JSX文件
import React from 'react';
import { Route, Switch } from 'react-router';
import Layout from './components/Layout';
import Home from './components/Dynamic/Home';
import Filter from './components/Dynamic/Filter';
import { AboutCampaign } from './components/Dynamic/AboutCampaign';
import { CampaignStart } from './components/Dynamic/CompaignStart';
import NotFound from './NotFound';
import {SignUp} from './components/Static/SignUp';
import { Login } from './components/Static/Login';
import Dashboard from './components/Dashboard'
import { withCookies } from 'react-cookie';
class App extends React.Component {
render() {
return (
<Switch>
<Layout>
<Route exact path='/' component={Home} />
<Route path='/filter' component={Filter}/>
<Route path='/Campaign/:campaignId' component={AboutCampaign}/>
<Route path='/SignUp' component={SignUp}/>
<Route path='/Login' component={Login}/>
<Route path='/Dashboard' render={(props) => <Dashboard {...props} isDashboard={true} /> } />
<Route path="" component={NotFound} />
</Layout>
</Switch>
);
}
}
export default withCookies(App);
Layout.JSX
import React from 'react';
import { Container } from 'reactstrap';
import Header from './Static/Header';
import Footer from './Static/Footer';
import TopNavigation from './Component/DashBoard/TopNavigation'
export default props => (
<div>
{props.isDashboard ? <Header /> : <TopNavigation /> }
<Container>
{props.children}
</Container>
<Footer />
</div>
);
当特定路由被调用但<Header/>
似乎是<TopNavigation />
props.isDashboard
和undefined
答案 0 :(得分:2)
这是因为您要向<Dashboard/>
组件发送道具
<Dashboard {...props} isDashboard={true} />
但不包含<Layout />
组件。
如果Layout组件需要接收isDashboard道具,则需要像这样传递它
<Layout isDashboard={true} />
由于Layout是父组件,因此如果子组件也需要道具,则可以随时将其传递给子组件。