const Course = (props) => { return ( <div> <Header></Header> <Content></Content> </div> ) }
Anima wrote: » const Course = (props) => { return ( <div> <Header></Header> <Content></Content> </div> ) } You're not sending any data to header or content?
Anima wrote: » You have to be explicit about passing data if you use props. Something like react context can be less so. You have to manually pass the props down each level which is a bit annoying and causes it's own set of problems with excess re-renders etc. For larger apps you probably would end up using redux or local contexts which adds a small bit of indirection but feels a bit better when using a lot of data.
<Content parts = {props.course.parts.map(part => <li> {props.course.parts.name} </li>)} />
Anima wrote: » <Content parts = {props.course.parts.map(part => <li> {props.course.parts.name} </li>)} /> This is probably not what you want to be doing. You should be passing data to components, not other components. At least most of the time anyway. In the mapping above, you're also not using the "part" argument but the same "props.courses.parts.name" each time. I'd try and think about the data flow of this app instead of writing code straight away. In general, react apps behave as if there is a data source that is static at the moment of rendering and you're just using this data to build a UI/DOM tree. Try and think about the components and the data and how they compose together. It should read pretty fluently and if not, then maybe something isn't represented as well as it could be.
S.M.B. wrote: » As Anima pointed out in the code snippet above, you should be only passing the parts data to your content component and the mapping should then be done in the latter.
course = { name: 'Half Stack application development', parts: [ { name: 'Fundamentals of React', exercises: 10 }, { name: 'Using props to pass data', exercises: 7 }, { name: 'State of a component', exercises: 14 } ] }
{props.course.parts.name}
{part.name}
enfant terrible wrote: » Not too familiar, have read Head First Javascript and done a few simple projects. So here: <Content parts = {props.course.parts.map(part => <li> {props.course.parts.name} </li>)} /> I should just be passing the parts data like so? <Content parts = {props.course.parts} /> And the mapping should be done in App component?
enfant terrible wrote: » For the mapping I was following the example given in the question which is as follows: const App = (props) => { const { notes } = props return ( <div> <h1>Notes</h1> <ul> {notes.map(note => <li>{note.content}</li>)} </ul> </div> ) } Thanks again for help
Talisman wrote: » Your data has the following structure: course = { name: 'Half Stack application development', parts: [ { name: 'Fundamentals of React', exercises: 10 }, { name: 'Using props to pass data', exercises: 7 }, { name: 'State of a component', exercises: 14 } ] } 'course.parts' is an array which is why you can use the map function to create the content for each entry. Inside the the anonymous function you are passing an entry from the 'parts' array and giving it the label 'part'. Each entry is an object with two keys, 'name' and 'exercises', so within the function you are only going to reference 'part.name' and/or 'part.exercises'. So instead of referencing the content as{props.course.parts.name} I would expect the function to instead use{part.name}