I'm trying to learn React by building a very basic "portfolio" site with react-router. My main components are: , , and . On the 'Work' page you should be able to see all of the project titles and types. Clicking on a specific project should route you to that project's detail page, which will include the rest of the project's details. How can I pass the props of to ? My structure:
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'
}
];
Since I don't ever explicitly use the component (its only called in the Routes), I'm not sure how to pass all of the project information to . Is there an easy way to do this, or should I think about structuring the app differently? Sorry for such a long post--this is my first time using React. Any help would be much appreciated.