【发布时间】:2016-06-28 10:13:52
【问题描述】:
我被困住了。我在单独的文件中有几个单独的组件。如果我将它们呈现在 main.jsx 中,如下所示:
ReactDOM.render(<LandingPageBox/>, document.getElementById("page-landing"));
ReactDOM.render(<TopPlayerBox topPlayersData={topPlayersData}/>, document.getElementById("wrapper profile-data-wrapper"));
ReactDOM.render(<RecentGamesBox recentGamesData={recentGamesData}/>, document.getElementById("history wrapper"));
一切正常,但我想知道这是否是一个好习惯?也许可以做一些像只有一个 ReactDom.render 这样的事情:
ReactDOM.render(<LandingPageBox recentGamesData={recentGamesData} topPlayersData={topPlayersData}/>, document.getElementById("page-landing"));
我尝试了不同种类的 LandingPageBox 变体以以某种方式包含其他两个组件,但没有运气。它们有时会呈现在页面之外等等。我认为它应该看起来像这样:
import React from 'react';
import RecentGames from '../RecentGames/RecentGames.jsx';
import TopPlayers from '../TopPlayers/TopPlayers.jsx';
import PageTop from './PageTop.jsx';
import PageBottom from './PageBottom.jsx';
class LandingPageBox extends React.Component {
render() {
return (
<body className="page-landing">
<PageTop>
<TopPlayers topPlayersData={this.props.topPlayersData} />
</PageTop>
<PageBottom>
<RecentGames recentGamesData= {this.props.recentGamesData}/>
</PageBottom>
</body>
);
}
}
export default LandingPageBox;
但此代码仅呈现 PageTop 和 PageBottom,没有播放器或游戏组件。
所以我的问题是,如何设置 LandingPageBox 文件,以便 TopPlayers 组件将呈现在 PageTop 组件中,RecentGames 组件将呈现在 PageBottom 组件中?谢谢。
【问题讨论】:
-
你为什么不用
this.props.children? -
我尝试阅读它,但我就是无法理解这个概念。如果我有这个例子中的两个,React 如何知道使用哪个孩子?另外也许我也不清楚一件事,那就是应该先渲染页面,然后在其中渲染这两个组件,因为页面组件有容器,这些组件必须放在其中。
标签: javascript reactjs