我试图通过建立一个非常基本的“组合”网站与反应路由器来学习反应。我的主要组件是:,,和。在“工作”页面上,您应该能够看到所有的项目标题和类型。点击一个特定的项目应该将您引导到该项目的详细信息页面,其中将包含该项目的其他细节。我怎么能通过道具? 我的结构:反应路由器:将道具传递给儿童
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