我試圖通過建立一個非常基本的「組合」網站與反應路由器來學習反應。我的主要組件是:,,和。在「工作」頁面上,您應該能夠看到所有的項目標題和類型。點擊一個特定的項目應該將您引導到該項目的詳細信息頁面,其中將包含該項目的其他細節。我怎麼能通過道具? 我的結構:反應路由器:將道具傳遞給兒童
main.js
/*
Routes
*/
var routes = (
<Router history={createHistory()}>
<Route path="/" component={App}>
<IndexRoute component={Work} />
<Route path="work" component={Work} />
<Route path="work/:id" component={ProjectDetail} />
<Route path="about" component={About} />
</Route>
</Router>
);
ReactDOM.render(routes, document.getElementById('main'));
App.js
/*
App
*/
class App extends React.Component {
render() {
return (
<div>
<h1>App</h1>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/work">Work</Link></li>
<li><Link to="/about">About</Link></li>
</ul>
{this.props.children}
<footer>footer</footer>
</div>
)
}
}
export default App;
Work.js
/*
Work
<Work/>
*/
import PROJECTS from '../PROJECTS';
class Work extends React.Component {
render() {
return (
<div>
<p>Work</p>
<ul>
{/* Need to loop of all projects */}
{PROJECTS.map(project => (
<li key={project.id}>
<Project project={project} />
</li>
))}
</ul>
</div>
)
}
}
export default Work;
Project.js
/*
Project
<Project/>
*/
class Project extends React.Component {
render() {
var project = this.props.project;
var linkTo = "/work/" + project.id;
return (
<Link to={linkTo}>
{project.title}
<span> </span>
{project.type}
</Link>
)
}
}
export default Project;
ProjectDetail.js
/*
ProjectDetail
<ProjectDetail/>
*/
class ProjectDetail extends React.Component {
render() {
return (
<div>
{/* THIS IS THE INFORMATION I NEED ACCESS TO */}
{this.props.project.title}
{this.props.project.description}
{this.props.project.type}
{this.props.project.technologies}
</div>
)
}
}
export default ProjectDetail;
PROJECTS.js
module.exports = [
{
id: 0,
title: 'Project 0',
description: '0 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod dolor nobis qui est magnam magni, voluptatibus optio beatae tempore ducimus dicta ratione, iure explicabo vero, sed iusto sunt minima earum.',
type: 'website',
technologies: 'blah, blah, blah'
},
{
id: 1,
title: 'Project 1',
description: '1 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod dolor nobis qui est magnam magni, voluptatibus optio beatae tempore ducimus dicta ratione, iure explicabo vero, sed iusto sunt minima earum.',
type: 'website',
technologies: 'blah, blah, blah'
},
{
id: 2,
title: 'Project 2',
description: '2 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod dolor nobis qui est magnam magni, voluptatibus optio beatae tempore ducimus dicta ratione, iure explicabo vero, sed iusto sunt minima earum.',
type: 'website',
technologies: 'blah, blah, blah'
}
];
因爲我從來沒有明確地使用組件(其唯一堪稱路線),我不知道怎麼打發所有的項目信息。有沒有簡單的方法來做到這一點,或者我應該考慮以不同的方式構建應用程序? 對不起,這是一個很長的文章 - 這是我第一次使用React。任何幫助將非常感激。
正如你的問題所展示的那樣,它有點寬泛,而且有很多代碼需要篩選。你能把它縮小到一個你想達到的小例子嗎? –
我只是想顯示ProjectDetail組件的數據。沒有其他組件會調用它。我不知道如何從父母那裏拉出道具。 – olafsadventures
[如何將道具傳遞給{this.props.children}]可能的重複(http://stackoverflow.com/questions/32370994/how-to-pass-props-to-this-props-children) – azium