From this code, (that works). I use map to render the array named parts. code in sandbox
import React from 'react'
const Header = (props) => {
//console.log(props)
//I pass the whole object (course) as props and use only name
return (
<div>
{ <h1>{props.course.name}</h1> }
</div>
)
}
const App = () => {
const course = {
id: 1,
name: 'Half Stack application development',
parts: [
{
name: 'Fundamentals of React',
exercises: 10,
id: 1
},
{
name: 'Using props to pass data',
exercises: 7,
id: 2
},
{
name: 'State of a component',
exercises: 14,
id: 3
}
]
}
return (
<div>
<Header course={course} />
<ul>
{course.parts.map(part =>
<li key={part.id}>
{part.name} {part.exercises}
</li>
)}
</ul>
</div>
)
}
export default App
I want to define a separate component responsible of rendering the contents of the array parts.
parts: [
{
name: 'Fundamentals of React',
exercises: 10,
id: 1
},
{
name: 'Using props to pass data',
exercises: 7,
id: 2
},
{
name: 'State of a component',
exercises: 14,
id: 3
}
I tried this. I checked for the props with console.log, and they are there. But I get the error
ReferenceError course is not defined.
There is something I am doing wrong, but I can't figure out what.
import React from 'react'
const Header = (props) => {
//console.log(props)
//I pass the whole object (course) as props and use only name
return (
<div>
{ <h1>{props.course.name}</h1> }
</div>
)
}
const Courses = (props) => {
console.log(props)
//I pass the whole object (course) as props
return (
<div>
<ul>
{course.parts.map(part =>
<li key={part.id}>
{part.name} {part.exercises}
</li>
)}
</ul>
</div>
)
}
const App = () => {
const course = {
id: 1,
name: 'Half Stack application development',
parts: [
{
name: 'Fundamentals of React',
exercises: 10,
id: 1
},
{
name: 'Using props to pass data',
exercises: 7,
id: 2
},
{
name: 'State of a component',
exercises: 14,
id: 3
}
]
}
//const Result = course.parts.map(parts => <li key={parts.id}>{parts.name}</li>)
//console.log(Result)
return (
<div>
<Header course={course} />
<Courses course={course} />
</div>
)
}
export default App
course.parts.mapshould beprops.course.parts.map