gpt4 book ai didi

javascript - 从 Route 发送后获取 React Props

转载 作者:行者123 更新时间:2023-12-01 01:05:33 26 4
gpt4 key购买 nike

我正在从此Link中提到的路线发送React组件

<Route path='/StartCompaign' render={(props) => <CampaignStart {...props} isDashboard={true} /> } />

我正在另一个组件中获取 Prop ,例如

{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);

布局.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 />在 react 应用程序中,当调用特定路由时,但 props.isDashboard似乎undefined

最佳答案

这是因为您正在向 <Dashboard/> 发送 Prop 组件

<Dashboard {...props} isDashboard={true} />

但不是<Layout />成分。如果布局组件需要接收 isDashboard 属性,您需要像这样传递它

<Layout isDashboard={true} />

由于 Layout 是父组件,如果子组件也需要 props,您可以轻松地将它们传递给子组件。

关于javascript - 从 Route 发送后获取 React Props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com