将子组件 class 作为 props 传递给 React 中的父组件
Passing child component class as props to parent component in React
如何在 React 中将子组件 class 作为 props 传递给父组件。
我的父组件代码:
import React, { Component } from 'react';
import { DashBoard } from './DashBoard/DashBoard';
import { DashBoard } from './Details/Details';
class ContentArea extends Component {
render() {
return (
<div className="content-wrapper">
<DashBoard />
<Details />
</div>
)
}
}
export default ContentArea;
我的子组件代码:
import React, { Component } from 'react';
export class DashBoard extends Component {
render() {
return (
<h1>This is DashBoard Page.</h1>
);
}
}
还有我的 Route.js 代码:
import React, { Component } from 'react';
import { render } from 'react-dom';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { App } from './App';
import { DashBoard } from './components/DashBoard/DashBoard';
import { Details } from './components/Details/Details';
export class AppRoute extends Component {
render() {
return (
<Router>
<div>
<Route exact path={'/'} component={App} />
<Route path={'/DashBoard'} component={DashBoard} />
<Route path={'/Details'} component={Details} />
</div>
</Router>
);
}
}
App.js :
import React, { Component } from 'react';
import SideBar from './components/SideBar';
import Footer from './components/Footer';
import Header from './components/Header/Header';
import ContentArea from './components/ContentArea';
export class App extends Component {
render() {
return (
<div className="skin-purple">
<Header />
<SideBar />
<ContentArea />
<Footer/>
</div>
);
}
}
现在我想将我的所有子组件作为 props 传递给父组件。并且在路由子组件时应该在父组件内部渲染。
谁能帮我实现这个目标?
由于 ContentArea 在 App 组件中并且您希望在其中呈现 DashBoard
和 Details
,您可以在 ContentArea
级别指定嵌套路由。
export class AppRoute extends Component {
render() {
return (
<Router>
<div>
<Route path={'/'} component={App} />
</div>
</Router>
);
}
}
import { DashBoard } from './DashBoard/DashBoard';
import { Details } from './Details/Details';
class ContentArea extends Component {
render() {
return (
<div className="content-wrapper">
<Route exact path={'/DashBoard'} component={DashBoard} />
<Route exact path={'/Details'} component={Details} />
</div>
)
}
}
export default ContentArea;
如何在 React 中将子组件 class 作为 props 传递给父组件。
我的父组件代码:
import React, { Component } from 'react';
import { DashBoard } from './DashBoard/DashBoard';
import { DashBoard } from './Details/Details';
class ContentArea extends Component {
render() {
return (
<div className="content-wrapper">
<DashBoard />
<Details />
</div>
)
}
}
export default ContentArea;
我的子组件代码:
import React, { Component } from 'react';
export class DashBoard extends Component {
render() {
return (
<h1>This is DashBoard Page.</h1>
);
}
}
还有我的 Route.js 代码:
import React, { Component } from 'react';
import { render } from 'react-dom';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { App } from './App';
import { DashBoard } from './components/DashBoard/DashBoard';
import { Details } from './components/Details/Details';
export class AppRoute extends Component {
render() {
return (
<Router>
<div>
<Route exact path={'/'} component={App} />
<Route path={'/DashBoard'} component={DashBoard} />
<Route path={'/Details'} component={Details} />
</div>
</Router>
);
}
}
App.js :
import React, { Component } from 'react';
import SideBar from './components/SideBar';
import Footer from './components/Footer';
import Header from './components/Header/Header';
import ContentArea from './components/ContentArea';
export class App extends Component {
render() {
return (
<div className="skin-purple">
<Header />
<SideBar />
<ContentArea />
<Footer/>
</div>
);
}
}
现在我想将我的所有子组件作为 props 传递给父组件。并且在路由子组件时应该在父组件内部渲染。
谁能帮我实现这个目标?
由于 ContentArea 在 App 组件中并且您希望在其中呈现 DashBoard
和 Details
,您可以在 ContentArea
级别指定嵌套路由。
export class AppRoute extends Component {
render() {
return (
<Router>
<div>
<Route path={'/'} component={App} />
</div>
</Router>
);
}
}
import { DashBoard } from './DashBoard/DashBoard';
import { Details } from './Details/Details';
class ContentArea extends Component {
render() {
return (
<div className="content-wrapper">
<Route exact path={'/DashBoard'} component={DashBoard} />
<Route exact path={'/Details'} component={Details} />
</div>
)
}
}
export default ContentArea;